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.