Can you use optional parameters in code targeting .NET 3.5?
I'm looking to write a library that uses the new optional parameters feature of C# 4.0, but I want to target it to the 3.5 version of the framework. Is this possible?
Are optional parameters syntactic sugar in the same way that extension methods are?
With VS2010 RC, I was able to create a .NET 3.5 application that uses optional parameters in C#.
So yes, it's all about syntactic sugar.
VS 2010 supports optional parameters in C# for .NET 3.5 solutions. One caveat however, is that the command-line compiler will report errors.
So, if you have automated builds in running, - using NANT or something else using the command-line compiler - your builds will fail.
Like Jon Skeet I was getting "Feature 'optional parameter' cannot be used because it is not part of the 3.0 C# language specification". However in the RTM version of Visual Studio you can select Language Version to "default" in Project Properties->Build->Advanced. That got it to work for me.
You can use optional and named parameters in a targeted framework of a previous version as long the assemblies are build within a development environment for v4 (f.e. VS2010). But you should be aware of runtime compiling or publishing websites in this case the compiler of the targeted framework will be used. And because the compilers of the older frameworks doesn't understand the syntax of optional and named params it will results in compiler errors.