Sean Francis N. Ballais Sean Francis N. Ballais - 2 months ago 6
C# Question

Default parameter specifiers are not permitted error on C#

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?

Answer

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