C# 4.0 Feature - Named Arguments

Let’s discuss about the new feature in C# 4.0 “Named Arguments”. As seen in the last post regarding “Optional Arguments”, we would have wondered for the “Add” method we had three parameters. But If we wanted to pass the first and third parameters instead of second , Is this possible ? Lets see the code below
private int Add(int i, int j = 1, int k = 0)
{
return i + j + k;
}

private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show(Add(2, 3).ToString());
}

This will look like we are passing for value “i” and “j” and not “k”. So how can we achieve this ?
In C#4.0 we have a new feature called “Named Arguments” where we can pass arguments with the parameter name : . The above code would be rewritten as follows
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show(Add(2,k:3).ToString());
}

Hope this was cool feature. Please add your comments or suggestions.

Cheers!!!

Comments

Popular posts from this blog

Jesus - God Thinks of you.

ASP.NET 4.0 Feature - URL Routing

Tips on JQuery Intellisense in VS 2008