This blog is moved to

Saturday, February 28, 2009

C# 4.0 - Named Arguments

For methods having numerous arguments we tend to get confused. And this is also pain for others while reviewing the code. C# 4.0 gives us an opportunity to pass parameters with name.

How it works, let’s check it. Suppose you have a method like below,

public static class TestClass


public static void ParamMethod(string Name, double Salary,

string Department, int Age = 30)


Console.WriteLine("Name = {0}", Name);

Console.WriteLine("Age = {0}", Age);

Console.WriteLine("Salary = {0}", Salary);

Console.WriteLine("Department = {0}", Department);



Now the calling would look like,

TestClass.ParamMethod(Name:"Amal", Salary:1000.00,


You can alter the sequence of the parameters too,

TestClass.ParamMethod("Tupur", 2000.00,

Department: "IT", Age: 26);

No compiler overhead is associated with it.

Rule, Non-optional parameters has to be specified either sequentially or by name.

No comments: