typeof(T) vs. Object.GetType() performance

后端 未结 4 908
自闭症患者
自闭症患者 2020-12-08 10:41

Is anyone aware of any differences between typeof(T) where T : struct, for example, vs. t.GetType() where t is a System.Object?
ILdasm shows t

4条回答
  •  一生所求
    2020-12-08 11:22

    Well, sometimes in generic code, you know the compile time type from a type parameter T, without having an instance. Then you must use typeof(T).

    At other times, typically in non generic code, you might be interested in the runtime type of an object. Then you use GetType().

    So in some cases, depending on what you want to know, or what you can query for, you only have one option.

    And sometimes, you could choose.

提交回复
热议问题