Is there a Optional in C#?
Sub notify(ByVal company As String, Optional ByVal office As String = "QJZ")
How would you implement the above code in C#? I have seen optionalstr
optionalint
but what about other datatypes and custom object?
Is there a Optional in C#?
Sub notify(ByVal company As String, Optional ByVal office As String = "QJZ")
How would you implement the above code in C#? I have seen optionalstr
optionalint
but what about other datatypes and custom object?
You have to give the parameter a default value and it must be at the end (The last parameter).
public void Test(string param1, string optional = "") {
}
To put it into context:
public void Notify(string company, string office = "QJZ") {
}
public void MyMethod(string company, string office = "QJZ")
{
}
Remember that optional parameters have to come at the end of the signature and that if you have another method with the same name that doesn't take any optional parameters, the compiler will choose that other method by default and will not use the optional parameter one. For example, if you also had
public void MyMethod(string company)
{
}
and you called
MyMethod("company name");
The compiler will automatically use the MyMethod(string company)
overload.
Remove the word optional
and then it will take the default value. As long as you are using >= VS2010
See here There is similar question here
Take a look at the documentation for the C# reference: http://msdn.microsoft.com/en-us/library/dd264739.aspx