简体   繁体   中英

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

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

Isn't typeof(MyClass) a compile-time constant?

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´ta 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>))
  {
  }
} 

Isn't typeof(MyClass) a compile-time constant?

That particular expression is statically resolvable, yes, but typeof() is evaluated at execution time (because of generics), so the rule must be that a typeof() call isn't a compile-time constant.

I do wonder whether it was in C# 1.0, when there was no such argument to be made...

This is old but if someone is looking for a Workaround:

    class MyClass
    {
        public void MyMethod(Type targetType = null)
        {
            if(targetType == null)
            {
                targetType = typeof(MyClass);
            }
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM