-->

Can Visual Studio target earlier C# syntax in addi

2019-03-28 13:25发布

问题:

The easy part:

Targeting the .NET 2.0 framework in a Visual Studio 2010 project using the dropdown.

The hard part:

Is it possible to target a specific syntax version - for example var s = "hello world" is valid syntactic sugar in VS2008 and above, but would not compile in VS2005. Can VS2010 be configured to flag this at compile time?

回答1:

This can be done by specifying the language version in the project settings. To set the language version to C# 2.0 do the following

  • Right Click on the project and select "Properties"
  • Go to the "Build" Tab
  • Click the "Advanced" Button
  • Change the "Language Version" drop down to "ISO-2"

Here are the other selections and their meanings in Visual Studio 2010.

  • ISO-1: C# 1.0 / Visual Studio RTM and 2003
  • ISO-2: C# 2.0 / Visual Studio 2005
  • C# 3.0: C# 3.0 / Visual Studio 2008
  • default: C# 4.0 / Visual Studio 2010


回答2:

Yes. From the IDE, set:

Project Properties -> Build -> Advanced -> Language Version : ISO-2


回答3:

Yes, you can do this, as others have said - but it's not a perfect simulation of the C# 1 compiler, as I discovered while I was giving a presentation. It will spot "big" changes in syntax, but not some subtle changes in behaviour. For example, in C# 1 there's no method group conversion, so you couldn't do this:

delegate void Foo(string x);
void Bar(string y) {}
...
Foo foo = Bar;

... but simply setting the C# 4 compiler to target C# 1 doesn't pick this up.