c# - C# 上不允许出现默认参数说明符错误

标签 c# .net-3.5 default-parameters

当我构建我的项目时,VC# 提示不允许使用默认参数说明符。它引导我找到这段代码:

public class TwitterResponse
{
    private readonly RestResponseBase _response;
    private readonly Exception _exception;

    internal TwitterResponse(RestResponseBase response, Exception exception = null)
    {
        _exception = exception;
        _response = response;
    }

我的错误是什么?

最佳答案

错误是:

Exception exception = null

您可以转移到 C# 4.0 或更高版本,此代码将编译!

这个问题将帮助你:

C# 3.5 Optional and DefaultValue for parameters

或者您可以在 C# 3.0 或更早版本上进行两次重写以解决此问题:

public class TwitterResponse
{
    private readonly RestResponseBase _response;
    private readonly Exception _exception;

    internal TwitterResponse(RestResponseBase response): this(response, null)
    {

    }

    internal TwitterResponse(RestResponseBase response, Exception exception)
    {
        _exception = exception;
        _response = response;
    }
}

关于c# - C# 上不允许出现默认参数说明符错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14019083/

相关文章:

.net - 添加重复选项卡

javascript - 解构作为 Firefox 中的第二个参数

javascript - 如何使用对象解构处理嵌套的默认参数?

C++选择 'wrong'带默认参数的重载方法

c# - 'Oracle.DataAcces.Client.OracleConnection' 的类型初始值设定项抛出异常

c# - 框架升级到 3.5

c# - 在显示其他表单时禁用完整表单

c# - 不区分大小写的字符串搜索

c# - 百万对象创建的性能建议

c# - 如何使用 linq 或 lambda 表达式连接两个列表