Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it safe to use newer C# features when building for older .NET Framework versions?

I maintain a .NET 2.0 library (currently, using Visual Studio 2013).

I have used a code-inspection tool on the code of the library and was surprised when the tool suggested using ?? operator or lambdas instead of some of the code.

I was even more surprised when I applied suggested edits and the library was successfully built after that.

Why the library with Target Framework set to .NET Framework 2.0 can be built with newer features of C# in it's code?

Is it safe to use newer C# features when building for older .NET Framework versions? I mean, the users of the library can have .NET Framework 2.0 only and I want them to be able to use the library even if I use lambdas in it's code.

like image 392
Bobrovsky Avatar asked Dec 25 '22 13:12

Bobrovsky


1 Answers

.NET 2.0 and C# 2.0 are 2 different things. In your case you could perfectly fine have a .NET 2.0 library (which is targeting the CLR 2.0) and use the C# 3.0 compiler which supports for example lambda expressions.

So you should make a difference between your compiler version and the version of the CLR you are targeting. Since you are using VS 2013, you could use newer versions of the compiler.

Is it safe to use newer C# features when building for older .NET Framework versions?

Yes, perfectly safe. The generated assembly is still targeting the CLR 2.0 and will run without any issues with this older version of the CLR.

like image 132
Darin Dimitrov Avatar answered May 19 '23 21:05

Darin Dimitrov