Jump to content
  • Advertisement
Sign in to follow this  

Having trouble understanding how to use Optional/Named Parameters in C# 4.0

This topic is 2681 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

The only thing I can really seem to "get" when it comes to Optional Parameters, is that they are used mainly to help lower the usage of Method Overloads. I don't really get what they're used for, how to use them correctly, and WHY to use them.

As for Named Parameters, the only thing I can understand is that they allow you to use the parameters in a varied order, instead of going int a, then int b, then int c, you could use them by using b first, then c, then a, etc.

I'm still a beginner, so forgive me if these are stupidly simple concepts that I'm not grasping >_>.

If/when examples are provided, could you guys be so kind as to use regular methods/parameters. I haven't learned about lists, etc yet.

Share this post

Link to post
Share on other sites
I haven't dealt with named parameters in C#, but I've dealt with them in Python.

Typically the reasons for named parameters are:

* Code clarity: When looking at the calling code, it is more clear exactly what role each passed-in parameter plays
* Access to default values: Many languages contain default parameter values but not named parameters. In these setups there is no good way to use the default values of any parameter you want. it's kind of hard to explain, so here's some code.

// a function with default parameters
void SomeFunc(int a, int b=3, int c=4, int d=5);

void CallingCode()
// I can call SomeFunc using all parameters
SomeFunc(1, 2, 3, 4);

// I can take the default value of the 'd' parameter
SomeFunc(1, 2, 3);

// I can take the default values of 'c' and 'd'
SomeFunc(1, 2);

// and even the default values of 'b', 'c', and 'd'

// but how would I pass in values for ONLY 'c', but take the default parameters for 'b' and 'd'?
// this is impossible in C++, unless you use a hack, such as forcing the caller to specify some non-value that means "use the default"
// here '-1' means "use the default"
SomeFunc(1, -1, 3, -1);

// this is ugly, however, and often is impossible if the parameter needs to be able to take all ints, for example
// so named parameters lets you do it like this:
SomeFunc(1, c=3); // note this is not valid C++.. it's valid Python though

Share this post

Link to post
Share on other sites
Optional parameters are excellent not only for overloading methods but also preserving while extending existing functionality. I had to implement a new requirement that meant modifying an existing method with a new parameter, which I made optional so as to not break its use elsewhere. The value that worked prior to the new requirement is what I set the default property to and simply wrote the new method calls passing in the new custom value and in the target method body I was able to treat the new optional parameter uniformly so it was a very nice and tidy implementation.

Share this post

Link to post
Share on other sites

Ok, I believe I know how to use them now. I just need to figure out when these come in handy.

I the end, a lot of it is syntactic sugar, with the compiler doing the work in your place. Optional parameters prevent you from having to do a whole lot of stupid repetitious code to support different params.

What you previously would have seen, say if you wanted to support an optional method with an additional boolean defaulting to true, is lots of code like this:

void DoSomething(int withInt, bool withBool)

void DoSomething(int withInt)

The ability to provide optional parameters make this kind of convenience code unnecessary.

Named parameters on the other hand, consider this, if you have 5 optional parameters to a method, and you want to define just one of them... how would you do it? Named params solve this problem.

Hmmm, I realize that example probably didn't make a lot of sense... consider this:

void SomeMethod(int requiredInt,
int optionalInt = 42,
string optionalString = "",
string anotherOptionalString = "bob" )
// Do something

Without named params, if you wanted to specify anotherOptionalString, you would have to do:


With named params, you can however:

SomeMethod(1, anotherOptionalString: "notbob");

Notice that I still needed to handle the non-optional params in order.

Also, they do make the code easier to read.

For the record, C++ has had optional parameter support forever and frankly it is one feature I miss. That said, C++ also has a preprocessor, which makes such capabilities easy to implement.

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!