Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compile code using Tuple using Mono's mcs

I have some C# code using Tuples:

public class Test {
    static void Main() {
        Tuple<int, int> t = Tuple.Create(0, 1);
    }
}

I tried compiling using

mcs -debug+ -o Test.exe Test.cs

but it gives the error

Test.cs(3,9): error CS0246: The type or namespace name `Tuple' could not be found. Are you missing a using directive or an assembly reference?
Compilation failed: 1 error(s), 0 warnings

I thought it might be trying to compile against an old version of mscorlib which lacks tuples. Looking at the man page, it seems you specify the version using -sdk:4, but that doesn't work either:

$ mcs -sdk:4 Test.cs

Unhandled Exception: System.TypeLoadException: Type 'System.Dynamic.BinaryOperationBinder' not found in assembly 'System.Core, Version=3.5.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'                                                                                                                                                                         

(followed by a stack trace).

I am running:

$ mcs --version
Mono C# compiler version 2.10.8.1

on Ubuntu Precise. According to the documentation, Mono has supported .NET 4.0 since version 2.8, and in particular supports System.Tuple, so that shouldn't be the issue.

How do you compile code that uses Tuples?

like image 506
Mechanical snail Avatar asked Jan 19 '26 06:01

Mechanical snail


1 Answers

I would expect it to fail with mcs but work with dmcs. I've just installed Mono 2.10.9 on Windows, clean, and here were my results with your code (including using System; at the top):

c:\Users\Jon\Test>mcs Test.cs
    Test.cs(4,9): error CS0246: The type or namespace name `Tuple' could not be
    found. Are you missing a using directive or an assembly reference?
    Compilation failed: 1 error(s), 0 warnings

c:\Users\Jon\Test>dmcs Test.cs
    Test.cs(4,25): warning CS0219: The variable `t' is assigned but its value is
    never used
    Compilation succeeded - 1 warning(s)

The difference is that dmcs uses framework v4 by default whereas mcs uses v2. You can get it to work with mcs just by specifying the v4 framework:

mcs -sdk:4 Test.cs

Try that, and also double check that you really had the same problem when you used dmcs. I wouldn't be surprised if you'd seen that it wasn't a clean compile but didn't notice that it was a different message.

like image 200
Jon Skeet Avatar answered Jan 20 '26 19:01

Jon Skeet