Can Visual Studio target earlier C# syntax in addition to earlier .NET framework versions?

冷暖自知 提交于 2019-12-21 04:50:13

问题


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.



来源:https://stackoverflow.com/questions/4994762/can-visual-studio-target-earlier-c-sharp-syntax-in-addition-to-earlier-net-fram

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!