Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which features of C# 5.0 can be compiled to run on .net 4.0

Tags:

c#

c#-5.0

I expect that framework level updates (such as async) will not be able to be compiled to work on a lower framework, but will compiler differences (such as the foreach loop variable scope) work correctly when compiled with C# 5.0 against .net 4.0? Can anyone provide a definitive list?

Edit:

Rather than ask for a definitive list, which I take back as being a lot to ask for, would anyone be able to answer whether the compiler level changes (like foreach variable scope) will behave differently when using the C# 5.0 against an earlier framework version.

like image 251
Andrew Hanlon Avatar asked Mar 20 '12 15:03

Andrew Hanlon


2 Answers

All of them. There's nothing that really requires new runtime support - some features just require new libraries. But there's no requirement that you compile it against the .NET 4.5 libraries - you can also provide your own implementation of the same API.

For example, add the following file to your project to enable support for async/await in .NET 4.0 projects: https://gist.github.com/1961087

I haven't tried it with the caller info attributes, but I'm pretty sure those will work even if you define them yourself instead of using the ones coming with .NET 4.5.

The same trick works with class ExtensionAttribute in C# 3 to use extension methods with .NET 2.0, and you can even use the full LINQ-to-Objects in .NET 2.0 (LinqBridge)

like image 162
Daniel Avatar answered Nov 14 '22 14:11

Daniel


As far as features go, any syntax that works in 4.0 can be compiled in 4.0, even if you originally produced it in 5.0. Visual Studio 11 will inform you of code that will not work in 4.0 if you change the .NET Framework version in the project properties, so that would be my guide. If compiling to 4.0 is an issue, use VS 11 to edit, but leave framework compatibility to 4.5.

There are likely some newer FUD that will compile to an older version. This was true with previous incarnations, but it is hit or miss to use new syntax and try to compile backwards.

I don't know of a definitive list of language features (keywords, et al) and have not found one with search. I have not seen a 4.5 language specification in the wild either.

like image 37
Gregory A Beamer Avatar answered Nov 14 '22 12:11

Gregory A Beamer