3

When I build my project, VC# says Default parameter specifiers are not permitted. And it leads me to this code:

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

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

What could be my mistake?

Mike G
  • 4,232
  • 9
  • 40
  • 66
Sean Francis N. Ballais
  • 2,338
  • 2
  • 24
  • 42
  • What's the error message exactly? Which line? – dtb Dec 24 '12 at 08:44
  • 2
    Which version of Visual Studio and which .NET framework are you using? Does [this](http://stackoverflow.com/q/7822450/76217) help? – dtb Dec 24 '12 at 08:45
  • http://stackoverflow.com/questions/7822450/default-parameter-specifiers-are-not-permitted – Habib Dec 24 '12 at 08:46
  • 2
    Possible duplicate of http://stackoverflow.com/questions/4203959/optional-parameter-in-c-sharp – Satbir Dec 24 '12 at 08:55
  • You should accept the answer if it was useful!! or put a comment to see where is the problem with the answer! – Saw Dec 27 '12 at 18:40

2 Answers2

5

The mistake is:

Exception exception = null

You can move to C# 4.0 or later, this code will compile!

This question will help you:

C# 3.5 Optional and DefaultValue for parameters

Or you can make two overrides to solve this on C# 3.0 or earlier:

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;
    }
}
Community
  • 1
  • 1
Saw
  • 6,199
  • 11
  • 53
  • 104
1

This could happen if you are using .NET 3.5. Optional parameters were introduced in C# 4.0.

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

Should be:

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

Note how there is no default value for the exception variable.

Darren
  • 68,902
  • 24
  • 138
  • 144