tags:

views:

281

answers:

3

Can IL produced by C# 4.0 compiler run on CLR 2.0?

To clarify, I'm not asking about VS 2010 multi-targeting (where VS chooses the right compiler version), I want to know if csc.exe 4.0 supports multi-targeting...

+1  A: 

C# 4.0 uses the .NET CLR 4.0 so there will be no binary compatibility with the CLR 2.0. You will be able to target the 2.0 framework though.

Bob
what is binary comparability ?
Max Toro
Bob meant to say 'compatibility'
Ed Swangren
haha correct Ed, you know you can't spell when you can't rely on spell check. Thanks!
Bob
then, what does 'binary compatibility' mean ?
Max Toro
it means two different binareries are compatible with each other! They will work with each other. Same as the English meaning of compatible...
Mitch Wheat
Binary compatibility is the concept that you're asking about in the original question. In your specific example, CLR 4.0 would be binary compatible with CLR 2.0 if the same IL ran on both.
Jed Smith
ok, which are the two binaries we are talking about?
Max Toro
@maxtoroq: the binary you compile with the C# 4.0 compiler, and the 2.0 CLR.
Michael Petrotta
Jed, so csc.exe 4.0 can produce IL that CLR 2.0 won't understand ?
Max Toro
@maxtoroq: it can, but as Mitch points out in his answer, you'll likely be able to target the 2.0 CLR with the 4.0 compiler.
Michael Petrotta
In this case, a better definition of "binary compatible" is that you can treat one binary as the same type as the other without problems.
RCIX
You can target CLR 2.0 from the 4.0 compiler; simple in the IDE, or see example in my answer for csc.
Marc Gravell
+1  A: 

It depends on the compiler.

Indications so far seem to indicate that the C# 4.0 compiler will be able to target the .NET 2.0 runtime, generating IL that is compatible with that runtime and ensuring that you only reference assemblies that also run on .NET 2.0.

Note that targeting is a choice, though - and there's no guarantee that any other compiler will have the same feature.

Mostly, I guess that it's a decision that will be made by the team implementing the compiler.

For example, it's likely that the C# and VB.NET compiler teams will support multitargeting (though they might back off from that and leave it as a Visual Studio feature, where VS chooses different compilers for different targets).

Other vendors using the .NET framework may make other decisions.

Bevan
I know I can target CLR 2.0 with csc.exe 3.5, that's why I asked specifically about the C# 4.0 compiler and not VS. VS 2010 is beta2 now, so I guess the decision to support mutitargeting is done, although you are not sure about that ...
Max Toro
+5  A: 

If all else fails, try it. I've just tested the following in VS2010b2, compiled targeting 2.0:

using System;    
class Program {
    static void Main() {
        Write();
        Write(msg: "world");
        Console.ReadLine();
    }
    static void Write(string msg = "Hello") {
        Console.WriteLine(msg);
    }
}

This uses two new C# 4.0 language features, which use the meta-data that is also present in .NET 2.x/3.x/CLR 2; it executes fine on my regular (CLR 2) machine (my VS2010b2 is a VM). So I conclude "yes, for some features". Obviously if you use a framework-dependent feature (dynamic, etc) it isn't going to end so well.

Edit: re your comment; I've tried csc at the command-line, and by default this does target CLR 4; I'll try to see if I can get it to target CLR 2 (like VS obviously can). Unfortunately, it no longer includes the (faked, btw) command line in the build output window...

Update: some "in the know" people came back with:

Pass /nostdlib and a reference to the 2.0 mscorlib.dll.

And sure enough:

C:\Windows\Microsoft.NET\Framework\v4.0.21006>csc /nostdlib /reference:%SystemRo
ot%\microsoft.net\framework\v2.0.50727\mscorlib.dll /out:c:\my.exe /target:exe "
C:\Users\Marc\Documents\Visual Studio 2010\Projects\ConsoleApplication6\ConsoleA
pplication6\program.cs"
Microsoft (R) Visual C# 2010 Compiler version 4.0.21006.1
Copyright (C) Microsoft Corporation. All rights reserved.

works fine (exe works on my non-4.0 machine). Credit: Kevin Pilch-Bisson

Marc Gravell