views:

165

answers:

4

This is, probably, a very simple answer for someone. I have a method with an Optional Parameter like so;

public static Email From(string emailAddress, string name = "")
    {
        var email = new Email();
        email.Message.From = new MailAddress(emailAddress, name);
        return email;
    }

Now, I must target .Net 3.5 and it was my understanding that Optional Parameters are part of .Net 4. However, my project builds and I double checked the Properties - Application page which states 3.5 as the target framework. Then I found a article on MSDN saying it's a feature of C#4 in VS2010. (MSDN Article --> Named and Optional Arguments)

Can someone help clarify this for me. C#4 does not require .Net4? What are Optional Parameters ACTUALLY a part of?

Thank you.

A: 

C# 4.0 is included with Visual Studio 2010, and the C# compiler understands optional parameters. So yes, the C# 4.0 language definition is something different than .NET 4.0. I guess a method with optional parameters compiled for .NET 3.5 will show overloaded methods when opening with eg. VS 2008

KBoek
No, optional parameters do not morph into overloaded methods - as JaredPar says, the CLR has supported optional parameters since 1.0.
Joel Mueller
+12  A: 

Optional parameters have been supported in the CLR since 1.0. Languages like VB.Net's have been using them since the start. While the first version of C# to support them is 4.0, it can still generate valid code for a 2.0 CLR and in fact does so. Hence you can use default parameters in 2010 if you are targeting the 3.5 CLR (or 2.0, 3.0, etc ...)

This type of support is not limited to default parameters. Many new C# features can be used on older version of the framework because they do not rely on CLR changes. Here are a few more which are supported on CLR versions 2.0 and above

  • Named Arguments: Added C# 4.0
  • Lambda Expressions: Added C# 3.0
  • Auto Properties: Added C# 3.0
  • Extension Methods: Added C# 3.0
  • Co/Contra Variance: Added C# 4.0
JaredPar
@JaredPar: are there any other options from VS2010 that can be applied to .NET 3.5 in the same way?
Yuriy Faktorovich
@Yuriy added some more.
JaredPar
@JaredPar How does the IDE accomplish this? I've been looking at the CSC file and can't find an argument which specifies which version to build to.
Yuriy Faktorovich
@Yuriy I'm less knowledgeable about how the IDE works here but at least for some features the IDE doesn't care what framework you're targeting and will just suggest new features.
JaredPar
+4  A: 

If you compile that up and examine the output using a tool like ILDASM you'll see that the optional parameter is simply implemented by adding an attribute to the metadata that describes the method's formal parameters. As long as that attribute class is available on the target platform there should be no problem with using the emitted code on a downlevel platform.

Eric Lippert
+1  A: 

The language version is independent of the framework version. For C# they happen to run mostly in parallel, i.e. C# 4 and framework 4.0 came with Visual Studio 2010. (However there is no 3.5 version of C#.)

With VB the version numbers are different, as VB 7 was the first VB.NET version. So, VB 10 comes at the same time as framework 4.0.

The optional parameters is a language feature introduced in C# 4. When you use VS 2010 you use C# 4, even if you target framework 2.0, so you can use all C# 4 features.

Guffa
Unless the features that you use require types that did not exist in the 2.0 framework. If the compiler cannot find an implementation of the DLR classes then you cannot use "dynamic" for example.
Eric Lippert