c# - 可选的 C# 参数问题

标签 c#

我有这个方法

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/

相关文章:

UserControl 的自定义 Text 属性的 C# 自定义 TextChanged 事件处理程序?

c# - 可移植独立C#编译器

c# - 应用程序调用了为不同线程编码的接口(interface)

c# - 如何更改 Xamarin.Forms UWP 应用程序的强调色?

c# - GridView 固定 header 溢出容器

c# - 自定义setter添加多对多关系.net core

c# - 读\写结构化二进制文件

c# - Lambda\Anonymous 函数作为参数

c# - 使用 DLLImport 时异常如何工作

c# - 我应该如何运行后台服务来为 Windows 世界中的网站创建导出文件?