Discover the difference among types: Is operator, typeof keyword and GetType Method

Source: Internet
Author: User
  • Is Operator-Shows if object X is of Type Y. if this condition is true, then you can cast the instance to the given type. this accounts for inheritance. for example, all types (such as system. int32) inherit from system. object. and 'is' check if we can cast that type safety to this type. also we can use the ''as''
  • Keyword when we want to see if we can cast form one type to another, if the cast is not valid then the ''as ''operator will return null.

     

  • GetType () method-A Method on system. object (and therefore available to all types) that returns the type of the object. the type class has a "fullname" property which returns a string of the name. notice that you can tell the name of immediate type here, But it doesn' t inheritance. for example, for an object on type "system. int32 "the" is "keyword accounts for the current type (INT) and the base type (object ). getType requires an instance and returns a type.
  •  

  • Typeof keyword-Takes a class an returns a type (almost the inverse of typeof ).
  •  

    "Instance. GetType () = typeof (class)" is not always equal "instance is Class". There will one problem when we use inheritance.

    I. e:

    Basepage inherits form page, and basepage inherits also from object, so if we

    Test for (New basepage (). GetType () = typeof (object) It'll return false because

    The types are different, but when we test using the is Operator it's true.

    (New basepage () is object) is true because (New basepage () is an object of Type basepage, and also a page and an object. the types might be different, but is operator checks whethere we can cast safely to this type.

     

    Notice:

    This same pattern can be applied to both value types (like a system. int32) and reference types (like an xmldocument ):

     

    Typeof ()Is an operator andGetType ()Is a method defined in system. Object an both of them returns and object of system. type.

    System. type. GetType is more than 7 times slower (on my dual core machine) than typeof.

    There are times when GetType is useful, but if the type is well known to you then use typeof.

    Use:

    Type Atype = typeof (datetime );

    Instead:

    System. type. GetType ("system. datetime ");

    By using the is operator, GetType method, and typeof keyword, we can define tively manage type information and solve several problems in various ways.

    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.