3

I'm developing an application whose target framework is version 3.5. But while looking at the code, I found a method using default parameters:

public void Contact(string name, string email, string phone, string phoneAreaCode = "")
{
    //...
}

and got confused.

The language features are independent of the framework version? What is the relation between both and why is this the code above possible?

EDIT: I have created 2 projects (a class library and a console) in VS2010, both tageting the .NET 2.0 Framework. On the class library, I've created a method with an optional string parameter. I've used it in the console app with no problems, with and without passing the parameter. Does this have anything to do with VS2010? And by "VS2010" you mean C# compiler 4.0?

1
  • It is also worth noting that C# 3s automatic properties can be used when targeting .NET 2 with Visual Studio 2008/2010. Commented Sep 14, 2011 at 19:51

3 Answers 3

4

The compiler emits the information, but the 3.5 runtime doesn't use it - it just gets ignored.

See this blog post, and these SO questions - one, two.

In essence, the 3.5 runtime sees this:

public void Contato(string nome, string email, string telefone, string ddd)
{
  //...
}
Sign up to request clarification or add additional context in comments.

3 Comments

I have created 2 projects (a class library and a console) in VS2010, both tageting the .NET 2.0 Framework. On the class library, I've created a method with an optional string parameter. I've used it in the console app with no problems, with and without passing the parameter (despite the linked article). Does this have anything to do with VS2010?
@Raphael - Comments are not a good place for code samples. If you have another question, why not ask a new one?
The second question linked on this answer answers the question on the comment. Contrary to what this answer says, a 3.5 code CAN take advantage of the optional parameter.
1

The language features are dependant on what version of Visual Studio you are using. The .Net Framework dictates what .Net functions and classes are available to you.

The code above is possible because you are using Visual Studio 2010. You can use all the features of the new code editor, regardless of what .Net version your assembly targets. But, the instant you try to use a .net 4.0 class or function in your .net 3.5 code, you will get a compiler error.

Comments

0

You must be using VS2010... because it supports it.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.