views:

140

answers:

5

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.

A: 

if you have 3.5 installed you should be able to select the framework in the project properties. Rightclick on the projectname -> properties

Digital Human
That's not the same as using the C# 3 compiler which comes with .NET 3.5 though...
Jon Skeet
True, it's not the same, but there is no need to use an earlier compiler. You cannot use language features that require a later framework if you target an older framework, you will get compile-time errors.
Tergiver
But you CAN use language features that are syntactic sugar and need no runtime support, which breaks the ability of programmers using the older version to recompile the code.
Ben Voigt
+4  A: 

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.

Jon Skeet
This is different from the target framework, and will check code for incompatibilities with older versions of the compiler? Such as (C# 3 compatible) omission of COM optional arguments, (C# 2 compatible) lambdas, extension methods, etc.
Ben Voigt
@Ben: It does give errors if you try to use new C# 4 features, yes - but it has *some* C# 4 behaviour (see example above). I haven't checked what it does with field-like events - I wouldn't be surprised to see the new behaviour there, too.
Jon Skeet
I confirm that you can build a proj that use ToolVersion = 4.0 on a server without a 4.0 compiler. It will say "Project file contains ToolsVersion="4.0", which is not supported by this version of MSBuild. Treating the project as if it had ToolsVersion="3.5"."
Pierre-Alain Vigeant
+3  A: 

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.

Guffa
Also, even if you target 3.5 with Visual Studio 2010, You can still use most of the new syntax features of C# 4, like better type inference, optional parameters and such and still compile against 3.5.
Pierre-Alain Vigeant
@Pierre: which is not a good thing if you're working with collaborators using earlier versions
Ben Voigt
@Pierre-Alain: You're assuming that the OP *wants* to use the features of C# 4. Given that he's said he wants to use the "3.5 C# compiler" (which I take to mean "the C# compiler that comes with .NET 3.5" I would assume he wants to *prevent* the C# 4 features from being used.
Jon Skeet
@Jon: And you are assuming the opposite too. He didn't specify. All he is saying is "get the productivity of developing in VS2010".
Pierre-Alain Vigeant
@Pierre-Alain Vigeant: Well, he's said quite clearly that he wants to be able to use the .NET 3.5 compiler, and that he needs to be able to build on a machine which doesn't have .NET 4 (and therefore doesn't have the C# 4 compiler). That will prevent any C# 4 features from being available on the build server - so it isn't much of a stretch of the imagination to suggest that he wants to prevent those from being used in Visual Studio too. He's said considerably more than "get the productivity of developing in VS2010."
Jon Skeet
All right, +1 for master Skeet. I got a confirmation that my team indeed removed the optional parameters from our code since it wasn't building on the build server. I was using outdated information. Sorry for the confusion. Just delete the above comments. *pointy haired boss*
Pierre-Alain Vigeant
+1  A: 

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?

Ben Voigt
I need to throw this code over the fence to yet another build server than only has 3.5 binaries and not 4.0 binaries at all.
MatthewMartin
Jon Skeet's suggestion of `/langversion` is probably your best bet then.
Ben Voigt
A: 

You can have a project in VS2010 target the 3.5 framework (not the compiler).

Just double-click the properties folder in solution explorer (or right-click on the project name) and in the Properties page, on the Application tab, select the targeted framework. (You can select from Frameworks 4.0 down to 2.0.)

David Hoerster
@D Hoerster: It's perfectly possible to have projects which will open in both VS2008 and VS2010. You can't do it at the *solution* level, but you can do it at the *project* level, so long as you don't use any features which are only in one release and not the other. Have a look at the projects in Noda Time: noda-time.googlecode.com - we have separate solution files, but they load the same projects.
Jon Skeet
thanks. removed my error.
David Hoerster