Why default does not work with double parentheses?

Why this compiles:

return default(T);

but this does not:

return default((T));

The full method is

public static T PenultimateOrDefault<T>(this IEnumerable<T> items)
{
    if (items.Count() >= 2)
        return items.ElementAt(items.Count() - 2);
    else
        return default(T);
}

The errors for default((T)) are

; expected
Invalid expression term ')'
Type expected

So it looks that parser is confued by double parentheses.

Jon Skeet
people
quotationmark

Well, that's just not how the language is specified.

It's important to understand that default is like typeof - they're operators1, not method calls. It's not like the name of the type is an argument - it's an operand, and the operand is expected to just be the type name.

Section 7.6.13 of the C# 5 spec shows the construction of a default value expression:

default-value-expression:
default ( type )

where type is just the name of a type or type parameter. You can no more put parentheses round it here as you could when using a generic argument, or declaring a variable:

(string) x = ""; // Invalid
var x = new List<(string)>(); // Invalid

1 It's never specified as "the default operator" in the spec; always "default value expression" - but it really is an operator in all meaningful senses, and it's listed with the other operators in the precedence table.

people

See more on this question at Stackoverflow