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.
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?
Posted by Deepak Vasudevan | Saturday, August 12, 2006 4:25:00 PM
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.
Posted by M. Aamir Maniar | Saturday, August 12, 2006 4:36:00 PM