我有这个方法
public static void WriteErrorLog(LogEntry logEntry, string method, [Optional, DefaultParameterValue(0)] int? errorTypeID)
所以我希望我可以像这样调用方法
WriteErrorLog(l, "text");
但无论如何我都会收到 Visual Studio 的错误 :(
No overload for method 'WriteErrorLog' takes 2 arguments
我错过了什么?
谢谢!
最佳答案
您不应该使用 [Optional, DefaultParameterValue(0)]
。相反,您应该使用 c 风格的默认参数语法:
public static void WriteErrorLog(LogEntry logEntry, string method, int? errorTypeID = 0)
此外,如果 errorTypeId
是一个 Nullable
,那么您不应该将默认值设为 null
吗?
public static void WriteErrorLog(LogEntry logEntry, string method, int? errorTypeID = null)
关于c# - 可选的 C# 参数问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10933894/