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?

Raphael
  • 1,847
  • 1
  • 17
  • 29
  • It is also worth noting that C# 3s automatic properties can be used when targeting .NET 2 with Visual Studio 2008/2010. – RichardOD Sep 14 '11 at 19:51

3 Answers3

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)
{
  //...
}
Community
  • 1
  • 1
Oded
  • 489,969
  • 99
  • 883
  • 1,009
  • 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 Sep 14 '11 at 20:58
  • @Raphael - Comments are not a good place for code samples. If you have another question, why not ask a new one? – Oded Sep 14 '11 at 20:59
  • 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. – Raphael Oct 03 '11 at 18:05
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.

EtherDragon
  • 2,679
  • 1
  • 18
  • 24
0

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

c0deNinja
  • 3,956
  • 1
  • 29
  • 45