The difference between C # GetType and typeof

Source: Internet
Author: User

typeof: The typeof operator is used to obtain the System.Type object for a Type.

operator to obtain a System.Type object of a certain type.

Type t = typeof (int);

GetType: Gets The Type of the current instance.

method to get the type of the current instance .

int i = 10;
Console.WriteLine (I.gettype ());

Difference:

    • Typeof () is an operator and GetType is a method
    • GetType () is a method of the base class System.Object, so it can be called only after an instance is established (after initialization)
    • Typeof () parameter can only be int,string,string, custom type, and cannot be an instance

    • Both GetType () and typeof () return references to System.Type.

      The difference between TypeOf () and GetType ():

      (1) TypeOf (): Gets the type of a class

      (2) GetType (): Gets the type of an instance of class

      Override a method's predecessor is this method in the parent class: abstract or virtual, override

Original link: http://www.cnblogs.com/tangself/archive/2009/12/06/1617885.html

The difference between C # GetType and typeof

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.