What happens to C# 4 optional parameters when compiling against 3.5?

Posted by Latest Microsoft Blogs on ASP.net Weblogs See other posts from ASP.net Weblogs or by Latest Microsoft Blogs
Published on Fri, 16 Apr 2010 00:14:05 GMT Indexed on 2010/04/16 2:03 UTC
Read the original article Hit count: 625

Filed under:
|
|
Here’s a method declaration that uses optional parameters: public Path Copy( Path destination, bool overwrite = false , bool recursive = false ) Something you may not know is that Visual Studio 2010 will let you compile this against .NET 3.5, with no Read More......(read more)

© ASP.net Weblogs or respective owner

What happens to C# 4 optional parameters when compiling against 3.5?

Posted by Bertrand Le Roy on ASP.net Weblogs See other posts from ASP.net Weblogs or by Bertrand Le Roy
Published on Fri, 16 Apr 2010 00:14:05 GMT Indexed on 2010/04/16 0:23 UTC
Read the original article Hit count: 625

Filed under:
|
|

Here’s a method declaration that uses optional parameters:

public Path Copy(
Path destination,
bool overwrite = false,
bool recursive = false)

Something you may not know is that Visual Studio 2010 will let you compile this against .NET 3.5, with no error or warning.

You may be wondering (as I was) how it does that. Well, it takes the easy and rather obvious way of not trying to be too smart and just ignores the optional parameters. So if you’re compiling against 3.5 from Visual Studio 2010, the above code is equivalent to:

public Path Copy(
Path destination,
bool overwrite,
bool recursive)

The parameters are not optional (no such thing in C# 3), and no overload gets magically created for you.

If you’re building a library that is going to have both 3.5 and 4.0 versions, and you want 3.5 users to have reasonable overloads of your methods, you’ll have to provide those yourself, which means that providing a version with optional parameters for the benefit of 4.0 users is not going to provide that much value, except for the ability to provide named parameters out of order. I guess that’s not so bad…

Providing all of the following overloads will compile against both 3.5 and 4.0:

public Path Copy(Path destination)
public Path Copy(Path destination, bool overwrite)
public Path Copy(
Path destination,
bool overwrite = false,
bool recursive = false)

© ASP.net Weblogs or respective owner

What happens to C# 4 optional parameters when compiling against 3.5?

Posted on Dot net Slackers See other posts from Dot net Slackers
Published on Fri, 16 Apr 2010 00:00:00 GMT Indexed on 2010/04/16 1:03 UTC
Read the original article Hit count: 625

Filed under:
Heres a method declaration that uses optional parameters: public Path Copy( Path destination, bool overwrite = false, bool recursive = false) Something you may not know is that Visual Studio 2010 will let you compile this against .NET 3.5, with no error or warning. You may be wondering (as I was) how it does that. Well, it takes the easy and rather obvious way of not trying to be too smart and just ignores the optional parameters. So if youre compiling against 3.5 from Visual Studio 2010,...

Did you know that DotNetSlackers also publishes .net articles written by top known .net Authors? We already have over 80 articles in several categories including Silverlight. Take a look: here.



Email this Article

© Dot net Slackers or respective owner

Related posts about .NET

Related posts about ASP.NET