I'm using visual studio 2008 / C# on a project. We're using .NET 2.0, and whatever version of C# was released in 2.0. I can set my project to use the .NET 2.0 framework in VS 2008, but I can't figure out where to select my C# compiler. Generally this would be a problem but about 90% of my day is spent in JS, so I keep accidentally leaving in a var
statement when declaring variables in C#. This compiles fine on my machine and VS reports no errors, then later it breaks the build. Is there any way to use the older C# compiler from VS 2005 (sorry I don't know what version # that is) in VS 2008?
You can't tell it to use the C# 2 compiler, but you can tell it to restrict itself to the C# 2 language.
In your project properties, go to the Build tab, click on the "Advanced..." button at the bottom right, and there's a Language Version option there... you want ISO-2.
Note that this isn't perfect - it stops you using most non-C#-2 features, but some changes may still get through. For example, I believe it will still use the more powerful generic type inference available in C# 3 than the C# 2 version. It's worth being careful about that. Here's an example:
using System;
class Test
{
static void Main()
{
Foo("hello", new object());
}
static void Foo<T>(T first, T second)
{
}
}
The C# 2 compiler can't decide what to use for T
in the call to Foo
, because each argument is used entirely separately to determine type inference, and any contradictory results give a compile-time error. C# 3 uses each argument to add bounds on what T
can be, and only gives an error if the bounds conflict (or if a type parameter can't be determined). The C# 3 compiler when invoked in C# 2 mode still uses the C# 3 behaviour.
Is there any reason you can't switch your actual build to use the .NET 3.5 (C# 3) compiler though? There's no need to shackle yourself to C# 2 just because you're using .NET 2. Heck, with LINQBridge you can even use LINQ to Objects :)
In the project properties, go to the "Build" tab, then hit "Advanced", and change the Language Version to ISO-2 instead of C# 3. This still uses the new 2008 compiler, but prevents you from using C# 3.0 features, which should accomplish your goals here.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With