C# 4.0: Named Argument

Okay, we saw Optional Parameters, an upcoming feature of C# 4.0, in the last post and one reader of my blog asked me a question related to “Optional Parameters”. I don’t believe I have readers of my blog as well 🙂 but that was also one of my friend :p

So, in last post, we discussed an example of Employee class in which we passed some optional parameters in constructor:

public Employee(string firstName, string lastName, string qualification = "N/A", string middleName = "")

and I can simply call that like:

Employee emp= new Employee("Adil", "Mughal");

The great question was that Is there any way that we can skip qualification i.e. third parameter and give last parameter of middleName? He seems to be good reader or he might have listened to Anders Hejlsberg’s session 😉

The answer of this question is that Yes absolutely we can and that feature is called Named Argument in C# 4.0. We can simply do this like:

Employee emp = new Employee("Adil", "Mughal", middleName: "Ahmed");

Good enough to answer the query :). Now let’s do some changes with the Employee constructor and make lastName optional as well:

public Employee(string firstName, string lastName = "", string qualification = "N/A", string middleName = "")

Now I can instantiate object of Employee in quite simple and flexible ways

Employee("Adil", qualification:"BS");
Employee("ABC", lastName: "EFG", qualification: "BS");
Employee("XYZ", middleName: "MNO");

These upcoming features are really cool as they will improve productivity of cool enough to help C# developers avoid writing ‘n’ number of overloads though some of them are not new in the programming languages’ world.

Optional Parameters in C# 4.0

In the last post we saw dynamic typed object in C# and as we saw earlier the upcoming features of C# 4.0, Today we are going to look at Optional Parameters.

Let’s say I have a class Employee and I provide few overloads of constructor to enable make certain parameters as optional as follows:

 public class Employee
{
public string FirstName { get; set; }
public string LastName { get; set; }
public string Qualification { get; set; }
public string MiddleName { get; set; }

public Employee(string firstName, string lastName)
{
FirstName= firstName;
LastName= lastName;
Qualification= "N/A";
MiddleName= string.Empty;
}
public Employee(string firstName, string lastName, string qualification)
{
FirstName= firstName;
LastName= lastName;
Qualification= qualification;
MiddleName= string.Empty;

}
public Employee(string firstName, string lastName, string qualification,
string middleName)
{
FirstName= firstName;
LastName= lastName;
Qualification= qualification;
MiddleName= middleName
}
}

With C# 4.0, you need to create just one constructor for that and that is

public Employee(string firstName, string lastName,
string qualification = "N/A", string middleName = "")
{
FirstName= firstName;
LastName= lastName;
Qualification= qualification;
MiddleName = middleName;
}

as simple as that 🙂 and you can easily call that like Employee(“Adil”,”Mughal”);

This feature was available in some other languages but was for some reason not provided in C# till yet but now it’s available. This feature has good impact in COM interop which allows developers to skip missing parameters which we will hopefully see in later post(s). Finally, the compiler will always fill the optional parameters by their default given values, if you do not provide them. For instance, In our current case it will be:

Employee(“Adil”, “Mughal”, “N/A”, “”);

Simple but useful feature!

Dynamic Programming in C# 4.0

C# 4.0 supports Dynamic Programming by introducing new Dynamic Typed Objects. The type of these objects is resolved at run-time instead of compile-time. A new keyword dynamic is introduced to declare dynamic typed object. The keyword tells the compiler that everything to do with the object, declared as dynamic, should be done dynamically at the run-time using Dynamic Language Runtime(DLR). Remember dynamic keyword is different from var keyword. When we declare an object as var, it is resolved at compile-time whereas in case of dynamic the object type is dynamic and its resolved at run-time. Let’s do some coding to see advantage of Dynamic Typed Objects 🙂

A year back I wrote a code of setting property of an object using Reflection:

   1:  Assembly asmLib= Assembly.LoadFile(@"C:tempDemoClassbinDebugDemoClass.dll");
2: Type demoClassType = asmLib.GetType("DemoClass.DemoClassLib");
3: object demoClassobj= Activator.CreateInstance(demoClassType);
4: PropertyInfo pInfo= demoClassType.GetProperty("Name");
5: pInfo.SetValue(demoClassobj, "Adil", null);
Notice line 3-5 creates instance of ‘demoClassType’ and set property ‘Name’ to ‘Adil’. Now with C# 4.0 line # 3-5 can be written as simple as:

dynamic dynamicDemoClassObj = Activator.CreateInstance(demoClassType);
dynamicDemoClassObj.Name = "Adil";
Simple isn’t it? Let’s see a slide from Anders Hejlsberg’s session at PDC 2008:

Slide

From the above slide, you can call method(s) such as x.ToString(), y.ToLower(), z.Add(1) etc and it will work smoothly 🙂

This feature is great and provides much flexibility for developers. In this post, we explore the dynamic typed object in C# 4.0. We will explore dynamic in detail and other features as well in the coming posts. Of course there are pros and cons of dynamic programming as well but where C# is going is something like having features of both static languages and dynamic languages.