Thursday, November 5, 2009

C# 4.0 new features: Named and Optional arguments

Microsoft has introduced few new features in C# 4.0. I would like to discuss the below listed new features in this post.

1. Optional arguments
2. Named arguments

Optional arguments: This feature allows you to omit arguments when calling methods. This is done by defining a method with assigning default values to parameters. For better understanding, let's take a look at the below example.

We define a method called "SomeMethod" by proving default values to two of its parameters as shown.

public void SomeMethod(int a, int b = 50, int c = 100);

Now this method can be called in different ways as shown below.

SomeMethod(10, 30, 50); // This is a normal call as 3 arguments were passed

SomeMethod(10, 30); // This call is omitting parameter "c". This call is equalant to SomeMethod(10, 30, 100)

SomeMethod(10); // This call is omitting both "b" and "c". This call is equalant to SomeMethod(10, 50, 100)

As you see, we can omit any number of consecutive parameters from right to left. In the above examples we omitted parameter "c" alone and parameters "b" and "c" together. Do you see a way to omit the parameter "b" in the above example? If you call the method "SomeMethod" by passing one argument as shown below, the compiler assumes that the argument passed was for the first parameter i.e, "a". So we need a way to tell the compiler that this argument was passed to a particular parameter, in this case it is "b". This requirement was fulfilled by c# 4.0's another new feature called "Named Arguments". Let's take a closer look at this feature.

Named Arguments: This feature allows you to pass the arguments by the corresponding parameter names.

For example you can pass the arguments by name as shown below.
SomeMethod(a:10, b:30, c:50);

By this feature, we don't need to pass the arguments in the order of parameters defined in the method. We can rewrite the above method call as shown below.
SomeMethod(c:50, b:30, a:10);

And you can solve the above discussed problem (omitting the middle parameter "b") as shown below.

SomeMethod(a:10, c:50); // This call Omits the parameter "b".

Optional and Named arguments features can also be applied to constructors. This feature is mainly useful where you have methods that have many number of parameters. Because it is very difficult to call a method by passing the parameters in same order as the method was defined. There are many possibilities to make mistakes in the order. So, having named parameters and the ability to pass parameters in any order by explicitly referring the parameter names will make our life easier.

kick it on DotNetKicks.com
Shout it

5 comments:

Kiran said...

nice post. good explanation.

Kiran said...

Looks like execution of stored procedure with default parameters in T-SQL

Kiran said...

Named parameters are a great feature of C#4 because they increase code readability. jQuery and ASP.NET MVC both use named parameters in a sense and it works very well.

Kiran said...

Nice post. Very good internals. This is much like T-SQL procedure calls.

Kiran said...

I've been waiting for this feature in C# since i migrated from Delphi. I wonder why haven't they implemented it before?