Geeks With Blogs
Bill Osuch - Random geek notes

In Visual Studio 2010, you now have the ability to use optional parameters and named arguments. Optional parameters are declared with a default value in the method signature, and allow you to omit the arguments when invoking the method, while names arguments allow you to provide and argument using the name of the desired parameter, instead of depending on the position. This is the one I'm most interested in, since it's going to make for more readable code and less risk when modifying method signatures.

For example, say we define a method like this:

private void NewMethod(string name, int IdNumber = 0, bool isValid = true)

All of these are valid calls:

NewMethod("Bob", 1, false);
NewMethod("Jeff"); // Same as Jeff, 0, true

NewMethod(name: "Steve", IdNumber: 123, isValid: false);
NewMethod(name: "Steve", isValid: false, IdNumber: 123); //Notice the order has changed

Posted on Monday, November 8, 2010 2:26 PM Visual Studio 2010 | Back to top

Comments on this post: Optional parameters and named arguments in VS 2010

No comments posted yet.
Your comment:
 (will show your gravatar)

Copyright © Bill Osuch | Powered by: