Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
199 views
in Technique[技术] by (71.8m points)

c# - Is there a list of changes for C#4.0 that work in .Net 3.5?

I've been seeing a lot of C# 4.0 changes as of late. I really like some of them. Also though, I do not want to move on to .Net 4.0 for compatibility reasons just yet.

So, is there a comprehensive list of new C# 4.0 language features that will work on .Net 3.5 or lower?

As an example, do default parameters require the .NET 4.0 CLR, or are they a compiler feature? It's possible to use automatic properties (a C# 3.0 feature) and still target .NET 2.0, since that doesn't require Framework support, but not to use LINQ expressions, since that does require Framework 3.5.

Please don't say "of course C#4.0 won't work in .Net 3.5 cause it's older"

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The C# compiler in 4.0 ships with a new multitargeting feature. The short version is that it will produce an assembly with whatever metadata version is present in the reference that defines System.Object (usually mscorlib.dll). This allows you to use it to compile assemblies for 2.0 and 3.5, as well as various versions of Silverlight. This feature was introduced in support of the multitargeting in Visual Studio 2010.

Therefore, you can use the C# 4.0 compiler to compile 3.5 assemblies, and make use of whatever C# 4 features you want, so long as there is no particular dependency on 4.0 libraries. For example, named arguments and optional parameters will work fine, because they don't use any 4.0 framework features. Dynamic, however, does, and so out of the box it won't work on 3.5.

There is a post about this here, that covers the basics.

http://blogs.msdn.com/ed_maurer/archive/2010/03/31/multi-targeting-and-the-c-and-vb-compilers.aspx

The short list (from Ed's post) is:

  • named arguments and optional parameters
  • certain COM syntax improvements (e.g., "omit ref")
  • generic variance

Things that won't work include dynamic and no-pia (our two biggest outlays in terms of time spent implementing them). The former requires framework support and the latter requires runtime support.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...