According to C# specification in 10.4 Constants:
The type specified in a constant declaration must be sbyte, byte, short, ushort, int, uint, long, ulong, char, float, double, decimal, bool, string, an enum-type, or a reference-type. Each constant-expression must yield a value of the target type or of a type that can be converted to the target type by an implicit conversion (§6.1).
Why then I can't do following:
public class GenericClass<T>
where T : class
{
public const T val = null;
}
That should be possible, because:
where T : classmeans, thatThe type argument must be a reference type; this applies also to any class, interface, delegate, or array type(from MSDN)- it satisfies another words from specification: the only possible value for constants of reference-types other than
stringisnull.
Any possible explanation?
Titself what the spec calls a generic type parameter, not a reference type. - SLaksconsttostatic? - Federico BerasateguiwhereconstrainTto be a class reference type? - David R Tribble