C#上不允许使用默认参数说明符错误

Sea*_*ais 3 c# .net-3.5 default-parameters

当我构建项目时,VC#表示不允许使用Default参数说明符.它引导我到这个代码:

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

    internal TwitterResponse(RestResponseBase response, Exception exception = null)
    {
        _exception = exception;
        _response = response;
    }
Run Code Online (Sandbox Code Playgroud)

可能是我的错误?

Saw*_*wan 5

错误是:

Exception exception = null
Run Code Online (Sandbox Code Playgroud)

你可以转到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;
    }
}
Run Code Online (Sandbox Code Playgroud)