Why doesn't C# switch statement allow using typeof/GetType()?

后端 未结 10 2049
被撕碎了的回忆
被撕碎了的回忆 2020-12-15 08:56

As in this example:

switch ( myObj.GetType ( ) )
{
    case typeof(MyObject):
        Console.WriteLine ( \"MyObject is here\" );
        break;
}

10条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-15 09:08

    a switch in C# only works for integrals or strings. myObj.GetType() returns a Type, which is neither an integral or a string.

提交回复
热议问题