C # Methods--gettype () and typeof () differences

Source: Internet
Author: User

Gettype () and typeof () differences

When comparing objects, you need to know their types to determine whether their values can be compared. All classes inherit the GetType () method from System.Object and are often used with the TYPEO () operator.

Both return a reference to the Syetem.type.

So it is necessary to differentiate between the two:

1.Gettype () is a method inherited from System.Object that returns the type of the current object of type types; typeof (XX) is an operator, XX must be a specific class name, type name, etc., and cannot be a variable name.

2.Gettype () must be created for specific objects (instances) to be used,

The parameters of 3.typeof () can only be int,string types and custom types, and cannot be instances.

4.Gettype returns the type (type) of the instance, and TypeOf returns the Class (classes).

C # Methods--gettype () and typeof () differences

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.