.net 3.5默认参数说明符在c#上是不允许出错的



当我构建我的项目时,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和DefaultValue

或者您可以在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;
    }
}

如果您使用的是。net 3.5,则可能发生这种情况。可选参数在c# 4.0中被引入。

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

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

注意exception变量没有默认值

相关内容

最新更新