Tuesday, November 23, 2004

 

Optional Parameters with C#

If you are creating component or library, then you have to consider many aspects of component development. One of the aspects is trying to provide a feature, which is not supported by the language in which components or library is being created. One of such thing is creating optional method parameters in C#, Languages like VB.Net natively supports such kind of parameters. So if you want to create optional method parameters in C# for VB.Net here is the way out.

In the namespace System.Runtime.InteropServices there is a one attribute defined, OptionalAttribute Just apply it to the argument you want to make as optional. That’s it…

For example…

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

Any yes, remember for C# optional parameters are not optional…

This page is powered by Blogger. Isn't yours?