Named and Optional Parameters in C#

Share on TwitterShare on TumblrSubmit to StumbleUponSave on DeliciousDigg This

This article explains this new feature in C# 4.0


 Download source code for Named and Optional Parameters in C#




Among many new features in C# 4.0 we have Named and Optional Parameters as one of the enhanced feature, that has helped in bringing down the number of lines of code.

Named parameters enable you to specify a parameter by associating that with the parameter’s name rather than with the parameter’s position in the list of parameters.

Optional parameters enable you to omit or skip some parameters for some parameters list.

Lets See some examples

In case we have method called Add(int num1, int num2) we can call the same as


Add(num1:10, num2:20);

Add(10, num2:20);

Add(num2:20, num1:10);

All are same here in c# 4.0;

Let’s see this example

Optional parametrs:

In C# earlier in order to call any method we had to pass all the parameters defined for that method and in sequence but now with C# 4.0 we can omit them.

In old way

In New way

That’s it.. See how the number of lines of code is reduced.

Hope you all liked this article. Do let me know your comment.