C# performance question: typeof(MyClass) vs. this.GetType()

typeof(Foo) is a static type lookup; essentially it occurs at compile time, so you only get the explicitly named type.

GetType() is a dynamic type lookup; it’s a virtual method that gets called at runtime and will give you the exact type even if you are using polymorphism. So it’s “slower”, theoretically, but it’s giving you something you can’t get from typeof(T). If you need one or the other for your design, the speed isn’t going to be a factor.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)