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!



9 comments:
Lol, I'm a Dyanmics Ax programmer and I wondered all the time why this was available in X++ and not in C#... it's finally in there now. Great!
The really interesting question is: does the C# compiler "burn in" the default parameter values on the caller's side or is it choosing the right parameter value from the callee's signature at the call time?
This is relevant from the code versioning perspective, say, if v2.0 of the assembly has different default parameter values.
Cheers
Who said anything about only passing value types? A string is an object, you should be able to pass null as a default value... for example:
public Employee(string firstName, string lastName, string qualification = null, string middleName = null){ FirstName= firstName; LastName= lastName; Qualification= qualification; MiddleName = middleName;}
which would pass
Employee(“Adil”, “Mughal”, null, null);
I like the idea of optional parameters, but C#3.0 intializers solve this problem also for the most some part. You could opt to only provide mandatory arguments as a constructor. And set the rest using properties when needed.
Eg.
public Employee(string firstName, string lastName)
{
FirstName = firstName;
LastName = lastName;
Qualification = "N/A";
MiddleName = string.Empty;
}
And use it like this:
new Employee("myFirstName", "myLastName"){Qualification = "My Qual"};
@Icey: yes will only work for constructors.
Don't get me wrong, I'm also glad that we will get this feature.
I just wanted to clarify that the constructor problem is not so big. And that the value of optional parameters will be mostly is having less method overloads.
I don't think the optional parameter syntax offers any real advantage to the overload syntax on its own. I think something very important that is missing from this blog post is the concept of named parameters and their relationship to optional parameters. Together the two offer some capabilities that simple function overloading syntax does not. With named parameters and optional parameters together, one can now create much more flexible code with a lot less code that is very readable. As a former VB developer (now C# developer) I was dead set against optional parameters, but with the patterns that come about with named parameters I think it is a positive addition to the language.
@Jason: Yes you are right. The concept of named argument is mentioned in another post :)
Plese visit : http://adilamughal.blogspot.com/2009/04/c-40-named-argument.html
Great news! No more junky duplicated code just because something has to be overloaded 10x. What a waste of time to change the code in ten places! This has always been available in VB. Just common sense.
Correct me if i'm wrong the above functionality can be done
if the functionality of all overloaded functions is almost same else if the functionality it self is different according to method signature then i hope it shud b inscribed in a if else loop which is again looks like a performance head.
I have understood the syntax of it but can u xplain me how it will impove performance by any case...
Post a Comment