Use of C # as and is

Source: Internet
Author: User

  1. Type conversions
    • Any type can be converted to its base class type, complete with implicit conversion;
    • When any type is converted to its derived type, the conversion must be displayed. such as: (type name) object name;
    • The exact type of any object can be obtained using GetType;
    • Primitive types can use the Convert class to implement type conversions;
    • Other than string types have the parse method, which is used to convert the string type to the corresponding base type;
    • Conversion of value types and reference types is called boxing (boxing) or unboxing (unboxing);
  2. as/is Example
    • is conversion rule:
      • Check the compatibility of object types and return the result true (false);
      • Does not throw an exception;
      • If the object is null, just returns false;
        Example:
        1 Objecto ="ABC"; 2 if(O is string)//perform the first type compatibility check3 {  4     strings = (string) O;//perform a second type compatibility check and convert5MessageBox.Show ("The conversion was successful! "); 6 }  7 Else  8 {  9MessageBox.Show ("conversion failed! "); Ten}
    • As conversion rules:
      • Checks the compatibility of the object type and returns the result of the conversion, or null if incompatible;
      • Does not throw an exception;
      • If the result is judged to be null, then forcing the type conversion throws a NullReferenceException exception; Example:
        1 Objecto ="ABC"; 2 strings = O as string;//performs the first type compatibility check and returns the result3 if(s! =NULL)   4MessageBox.Show ("The conversion was successful! "); 5 Else  6MessageBox.Show ("conversion failed! ");

        As is less performing a compatibility check than is, performance can be a little bit higher.

Use of C # as and is

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.