Mono2.6 は C#4.0 が(不完全ながら)サポートされているようなので、Ubuntu 10.04 (Server Edition) にて試そうと思いました。しかし、
$ sudo apt-get install mono-devel
で導入されるのは今のところ mono2.4 です。こんなときはソースからがんばってビルド、というところですが、monoは依存パッケージも多いしなかなか大変そう。ということで、badgerportsをaptのリポジトリに追加することでaptでインストールします。
以下のページの通りなので、特に何も目新しいことはありません。
http://mono-project.com/DistroPackages/Ubuntu
リポジトリに追加
http://badgerports.org/
badgerports の真ん中のリンク "How do I use badgerports?" の説明を参考に、apt のリポジトリを追加します。/etc/apt/sources.list を開き、以下の1行を加えます。
deb http://badgerports.org lucid main
GPG鍵の追加
$ wget -q http://badgerports.org/directhex.ppa.asc -O- | sudo apt-key add -
monoのインストール
あとは普通に apt でインストールすれば mono 2.6 が入ります。
$ sudo apt-get update $ sudo apt-get install mono-devel mono-mcs
$ mono -V Mono JIT compiler version 2.6.3 (Debian 2.6.3-2~dhx1) ....
C#プログラムのコンパイル・実行
C#4.0 の新機能・オプション引数を使ったプログラム (test.cs) を以下のように書いてみます。
using System; namespace Example { public class Program { // 2点間の距離を返す。 // 2つ目の点の座標を省略すると、原点からの距離を求める。 static double Dist(int x1, int y1, int x2=0, int y2=0) { return Math.Sqrt((x2-x1)*(x2-x1) + (y2-y1)*(y2-y1)); } public static void Main(string[] args) { Console.WriteLine(Dist(1,1)); Console.WriteLine(Dist(1,1, -1,1)); } } }
コンパイルはmcsやgmcsコマンドでできます。C#4.0 用にdmcsというコマンドがあるという情報を見かけるのですが、どうも当方の環境には無いようです。mcs や gmcs で C#4.0 もコンパイルできてしまいます。
$ mcs test.cs
実行は mono コマンドです。.exe は省略できません。
$ mono test.exe 1.4142135623731 2