简体   繁体   中英

Is there any way to use the 2005 C# compiler in Visual Studio 2008?

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 :)

在项目属性中,转到“构建”选项卡,然后单击“高级”,并将语言版本更改为ISO-2而不是C#3.这仍然使用新的2008编译器,但阻止您使用C#3.0功能,这应该在这里实现你的目标。

Are different team members use different versions of Visual Studio? The ideal strategy is for everyone to use the same version of Visual Studio, even if this means you are stuck with VS2005.

I think a VS2008 MSDN license includes permission to run VS2005 for development, so you could run the two VS versions concurrently if you need VS2008 for other reasons.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM