Why doesn't C# allow a typeof as a default parameter?

I am not a IL expert, but seems that it calls a method at L_0005:

return typeof(int);

It´s the same of:

.maxstack 1
.locals init (
    [0] class [mscorlib]System.Type typeofvar)
L_0000: ldtoken int32
L_0005: call class [mscorlib]System.Type [mscorlib]System.Type::GetTypeFromHandle(valuetype [mscorlib]System.RuntimeTypeHandle)
L_000a: stloc.0 
L_000b: ldloc.0 
L_000c: ret 

You can see that it isn´t a constant writing type of code:

const Type constType = typeof(int);

That returns a error:

Constant initialize must be compile-time constant

From MSDN - Named and Optional Parameters:

A default value must be one of the following types of expressions:

  • a constant expression;

  • an expression of the form new ValType(), where ValType is a value type, such as an enum or a struct;

  • an expression of the form default(ValType), where ValType is a value type.


typeof does not necessarily return a compile time constant as it may return different results depending on context.


because it isn't necessarily a constant expression. your example features a typeof on a simple class but what if the class was generic? obviously this isn't constant by far:

class MyClass<T>
{
  public void MyMethod(Type targetType = typeof(MyClass<T>))
  {
  }
}