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

后端 未结 7 1193
渐次进展
渐次进展 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:41

    Yes, there is a difference if you have an inherited type from Customer.

    class VipCustomer : Customer
    {
      .....
    }
    
    static void Main()
    {
       Customer c = new VipCustomer();
       c.GetType(); // returns typeof(VipCustomer)
    }
    
    0 讨论(0)
提交回复
热议问题