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?
你必须给参数一个默认值,它必须在最后(最后一个参数)。
public void Test(string param1, string optional = "") {
}
把它放到上下文中:
public void Notify(string company, string office = "QJZ") {
}
public void MyMethod(string company, string office = "QJZ")
{
}
请记住,可选参数必须出现在签名的末尾,并且如果您有另一个同名的方法不带任何可选参数,编译器将默认选择另一个方法并且不会使用可选参数之一. 例如,如果你也有
public void MyMethod(string company)
{
}
你打电话给
MyMethod("company name");
编译器将自动使用MyMethod(string company)
重载。
尝试这个
void notify(string company ,string office ="QJZ"){}
查看 C# 参考的文档:http: //msdn.microsoft.com/en-us/library/dd264739.aspx