Wednesday, March 28, 2012

Optional parameters in C# 4.0

My friend shared this with me some time ago, I think this is quite useful.

In order to provide a few constructors, we can just do as follow:
public Employee(string firstName, string lastName,
            string qualification = "N/A", string middleName = "")
{
    FirstName= firstName;
    LastName= lastName;
    Qualification= qualification;
    MiddleName = middleName;
}

Call the constructor as follow:

Employee(“Adil”,”Mughal”);

I am thinking this way is good if we allow optional parameters, so that when we calls the method, we won't keep passing in null values. ^_^

No comments:

Post a Comment