2012-12-24 53 views
3

当我构建我的项目时,VC#表示不允许使用默认参数说明符。它导致我到这个代码:C#上不允许默认参数说明符错误

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

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

什么可能是我的错误?

+0

准确的错误信息是什么?哪条线? – dtb

+2

您使用的是哪个版本的Visual Studio和哪个.NET框架? [this](http://stackoverflow.com/q/7822450/76217)有帮助吗? – dtb

+0

http://stackoverflow.com/questions/7822450/default-parameter-specifiers-are-not-permitted – Habib

回答

5

的错误是:

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

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

+0

我试过这个解决方案,但这不起作用。 –

+0

@SeanfrancisBlalais - 你收到了什么错误? –