Usage and difference of As and is in C #

Source: Internet
Author: User
I. type conversion

1. Any type can be converted to its base class type, which is completed by implicit conversion;

2. Display conversion is required when any type is converted to its derived type. For example: (type name) Object Name;
3. Use GetType to obtain the exact type of any object;

4. You can use the convert class to convert basic types;
5. All types except string have the parse method, which is used to convert the string type to the corresponding basic type;

6. The conversion between the value type and the reference type is called boxing or unboxing );

2. is/as Example

Is conversion rules:

1. Check the compatibility of object types and return true (false );
2. No exception is thrown;
3. If the object is null, false is returned;

Example:

C # Code
  1. ObjectO ="ABC";
  2. If(OIs String)// Perform the first type compatibility check
  3. {
  4. StringS = (String) O;// Perform the second type compatibility check and convert
  5. MessageBox. Show ("Conversion successful! ");
  6. }
  7. Else
  8. {
  9. MessageBox. Show ("Conversion failed! ");
  10. }
Object o = "ABC"; if (O is string) // perform the first type compatibility check {string S = (string) O; // perform the second type compatibility check, and convert MessageBox. show ("conversion successful! ");} Else {MessageBox. Show (" Conversion failed! ");}

As conversion rules:

1. Check the compatibility of object types and return the conversion result. If not, null is returned;
2. No exception is thrown;

3. If the result is null, an nullreferenceexception exception will be thrown during forced type conversion;

Example:

C # code
    1. Object O = "ABC" ;
    2. string S = O as string ; // executes the first type compatibility check and returns the result.
    3. If (s! = null )
    4. MessageBox. Show ( "conversion successful! " );
    5. else
    6. MessageBox. Show ( "Conversion failed! " );
Object o = "ABC"; string S = O as string; // execute the first type compatibility check and return the result if (s! = NULL) MessageBox. Show ("conversion successful! "); Else MessageBox. Show (" Conversion failed! ");

Note: As performs a compatibility check less than is, and the performance may be slightly higher.

Related Article

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.