Can the IL created by the C # 4.0 compiler work on CLR 2.0?

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

To clarify, I am not asking about VS 2010 multi-targeting (where VS selects the correct version of the compiler), I want to know if csc.exe 4.0 supports multi-targeting ...

+6
c #
source share
3 answers

If all else fails, try. I 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); } } 

It uses two new features of C # 4.0, which use metadata, which is also present in .NET 2.x / 3.x / CLR 2; it works fine on my regular machine (CLR 2) (my VS2010b2 is a virtual machine). Therefore, I conclude "yes, for some functions." Obviously, if you use a structure-dependent function ( dynamic , etc.), it will not be so good.

Edit: repeat your comment; I tried csc on the command line, and by default this points to CLR 4; I will try to see if I can get it for the target CLR 2 (e.g. VS). Unfortunately, it no longer includes the command line (faked, btw) in the assembly output window ...

Update: Some β€œup to date” people have returned using:

Pass / nostdlib and a link to the 2.0 mscorlib.dll file.

And of course:

 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 runs on my machine other than 4.0). Credit: Kevin Pilch-Bisson

+7
source share

C # 4.0 uses the .NET CLR 4.0, so there will be no binary compatibility with CLR 2.0. However, you can target your environment to 2.0.

+1
source share

It depends on the compiler.

The readings so far show that the C # 4.0 compiler will be able to customize the .NET 2.0 runtime, generating an IL compatible with this runtime, and will ensure that you will only reference assemblies that also run on .NET 2.0.

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

Basically, I think this is a decision that will be made by the team implementing the compiler.

For example, it is likely that the C # and VB.NET compiler commands will support multitasking (although they can step back from this and leave it as a Visual Studio function, where VS selects different compilers for different purposes).

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

+1
source share

All Articles