What is the difference between myCustomer.GetType() and typeof(Customer) in C#?

后端 未结 7 1207
渐次进展
渐次进展 2020-12-02 07:11

I\'ve seen both done in some code I\'m maintaining, but don\'t know the difference. Is there one?

let me add that myCustomer is an instance of Customer

7条回答
  •  粉色の甜心
    2020-12-02 07:40

    GetType() is used to find the actual type of a object reference at run-time. This can be different from the type of the variable that references the object, because of inheritance. typeof() creates a Type literal that is of the exact type specified and is determined at compile-time.

提交回复
热议问题