« Home | Ways of Instantiation » | @Software-Architect.Net EMail Launched » | Got a bike » | Back @ Blogger » | Software-Architect.net Alpha has been launched for... » | Generics.Net » 

Sunday, March 26, 2006 

Making Optionals with C#

As being a component or library developer one has to consider many aspects of component development. One of the such aspect is providing a feature which is not natively supported by the language in which components is being developed. For example Optional parameters feature of VB.Net. Everyone knows that optional parameters are not possible in C#. Yes, that's true, well there is a way out. Just apply OptionalAttribute defined in the System.Runtime.InteropServices to the parameter that you want to make optional. That's it, you are done...

Here is an example...

public string GetName([Optional]string param)
if (param == null) //Check for null.
param = "Aamir"; //Set the default value.
return param;

Andyes, remember C# optional parameters are not optional in C#. This is the way of creating optional parameter for vb.net.

We can overcome this limitation by overloading right?

Yes, thats always true. But if you are creating a component on a comman platform for vast variety of users, you need to consider language specific attributes and impementations also. Some users may be more comfortable with optional parameters, then overloaded functions.

Post a Comment

About me

  • I'm M. Aamir Maniar
  • From Mumbai, Maharastra, India
My profile


Powered by Blogger
and Blogger Templates