Question

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?

Was it helpful?

Solution

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

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top