Mono で BigInteger を使う

Mono で BigInteger を使う方法です。 以下のコードでは BigInteger を使っています。mcs コマンドでコンパイルすると、コンパイルエラーになります。

// sample.cs
using System;

class Program {
    public static void Main() {
        BigInteger x = 10;
        Console.WriteLine(x + 10);
    }
}

mcs コマンドでコンパイルすると、以下のコンパイルエラーが表示されます。

$ mcs sample.cs
sample.cs(5,9): error CS0246: The type or namespace name `BigInteger' could 
not be found. Are you missing an assembly reference?
sample.cs(6,27): error CS0841: A local variable `x' cannot 
be used before it is declared
Compilation failed: 2 error(s), 0 warnings

BigInteger が見つからないようです。

C# コードに using System.Numerics を追加して、mcs コマンドに -r:System.Numerics を追加するとコンパイルできるようになります。

using System;
using System.Numerics; // 追加

class Program {
    public static void Main() {
        BigInteger x = 10;
        Console.WriteLine(x + 10);
    }
}

-r:System.Numerics オプションを追加して、コンパイルします。

$ mcs -r:System.Numerics sample.cs
$ mono sample.exe
20

BigInteger を使うことができました。

以下、まとめです。

  • C# コードに using System.Numerics を追加する。
  • mcs コマンドに -r:System.Numerics を追加する。