【问题标题】:Default parameter specifiers are not permitted error on C#默认参数说明符在 C# 上是不允许的错误
【发布时间】:2012-12-24 08:43:28
【问题描述】:

当我构建我的项目时,VC# 说不允许使用默认参数说明符。它把我带到了这段代码:

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

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

我的错误可能是什么?

【问题讨论】:

标签: c# .net-3.5 default-parameters


【解决方案1】:

错误是:

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;
    }
}

【讨论】:

    【解决方案2】:

    如果您使用的是 .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 变量没有默认值。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-12-10
      • 2013-12-27
      • 2011-09-17
      • 1970-01-01
      • 2014-12-10
      • 1970-01-01
      • 2014-09-28
      • 1970-01-01
      相关资源
      最近更新 更多