Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get VS2010 to use the 3.0 C# compiler instead of 4.0?

How can I get VS2010 to use the 3.5 C# compiler instead of 4.0?

It it even possible, or do I need to have a separate VS2008 solution file to coerce compilation with the 3.0 compiler?

(updated to fix compiler version #)

Update: My motivation is to get the productivity of developing in VS2010, but it needs to run on a build server that may not have a single bit of .NET 4.0 binaries on it.

like image 517
MatthewMartin Avatar asked Jul 23 '10 13:07

MatthewMartin


3 Answers

I don't believe you can do so. You can set a language version in the project properties, but that's not the same as using the C# 3 compiler from .NET 3.5. I believe there are subtle differences around type inference and overload resolution, for example, where the C# 4 rules will be followed instead of the C# 3 rules, even when building as C# 3. Here's an example of that:

using System;
class Test
{
    static void Main()
    {
        CallFunc(Demo);
    }

    static int Demo() { return 5; }

    static T CallFunc<T>(Func<T> func)
    {
        return func();
    }
}

This builds using the C# 4 compiler, even when you specify /langversion:3 - but it doesn't build with the C# 3 compiler.

You may find that you can tweak the project file to use the previous version of the compiler - but I don't know how Visual Studio would deal with that. I suspect it would get hideously confused.

EDIT: Okay, now you've given the motivation, I think you may want to do what we do for Noda Time. We have a VS2010 solution and a VS2008 solution file. They both load the same projects, which have a ToolsVersion attribute of 4.0. I haven't tried, but I believe that should build absolutely fine on a machine with only .NET 3.5.

You may well want to set the language version to 3 as well in your project files, to avoid accidentally using new features. It won't make your compatibility bullet-proof, but if you've got all your unit and integration tests building and running on a .NET 3.5-only continuous build server, you're unlikely to get bitten by the changes.

like image 69
Jon Skeet Avatar answered Nov 19 '22 01:11

Jon Skeet


You can't. Visual Studio 2010 uses the 4.0 compiler even if you target framework 3.5.

Also, there is no version 3.5 of the C# compiler.

like image 31
Guffa Avatar answered Nov 19 '22 00:11

Guffa


Can't be done. However, there's not much need for that as the C# 4 compiler is able to generate .NET 2.0-compatible assemblies (.NET 3.0 and 3.5 are just the 2.0 runtime engine + new compilers and libraries).

Are you sure you need the C# 3 compiler (ships with VS 2008 / .NET 3.5) or is having output compatible with it good enough?

like image 1
Ben Voigt Avatar answered Nov 19 '22 02:11

Ben Voigt