Can you use optional parameters in code targeting .NET 3.5?
Asked Answered
T

7

32

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?

Truthful answered 31/7, 2009 at 4:25 Comment(0)
F
26

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.

Frogmouth answered 1/3, 2010 at 9:38 Comment(0)
A
13

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.

Approbate answered 4/6, 2010 at 6:18 Comment(1)
Ya so the work around in this is to change your automated build to use MSBuild located in the 4.0 Framework Directory.Isocyanide
P
5

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.

Persuasive answered 12/7, 2010 at 14:45 Comment(0)
O
3

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.

Ornithine answered 21/4, 2011 at 9:34 Comment(1)
I think what Frans mentions is an important point. If you compile a desktop application which calls a method that uses optional parameters with a targeted framework of an earlier version, let's say 2.0, within VS2012, everything is fine. But you won't be able to build a web application with a targeted framework of an earlier version that calls the same method.Lukas
B
3

Go to project properties -> Build Tab -> click button 'Advanced' -> set language version to 'default' -> save -> be happy ;)

Brierwood answered 30/11, 2011 at 11:20 Comment(0)
S
2

I don't have VS2010 installed here to check, but I believe this would be purely a language feature, and therefore should be usable regardless of the framework being targeted.

Edit: Looking at this link (and a few others), it appears that optional parameters compile to method arguments with an [opt] attribute in the il. I don't know if this parameter existed in previous versions of the clr, but still my guess would be that it does.

Seafarer answered 31/7, 2009 at 4:27 Comment(2)
Yeah, I would think that this is the case, but I don't want to go through installing VS2010 when it isan't RTM yet if it doesn't give this to me.Truthful
C# 4.0 reuses the attribute that VB uses for that purpose, so it has been around since 1.0.Quackery
M
0

VB.NET has optional parameters if you want to use optional parameters in .NET 3.5.

Manofwar answered 31/7, 2009 at 4:29 Comment(1)
Yeah, I know. I love Optional parameters but hate VB. ;) Plus, having an app in 2 languages is kind of a kludge if you only need VB stuff for one or two classes.Truthful

© 2022 - 2024 — McMap. All rights reserved.