CLR via note 4.2 type conversion is and as difference

Source: Internet
Author: User

The IS and as operators are used for coercion of type conversions.

Is: Checks whether an object is compatible with other specified types and returns a bool value that never throws an exception

Object o = new Object ();

If (O is Label)
{
Label lb = (label) o;

Response.Write ("type conversion succeeded");
}
Else
{
Response.Write ("type conversion failed");
}

In the above code, the CLR will actually check the type of two objects, the IS Operation Mr. Foo Check once, if O is compatible with lable, then the (Label) O will be checked again, the efficiency is low, is not recommended to use

As: is the same as coercion type conversion, but never throws an exception, that is, if the conversion is unsuccessful, NULL is returned

Object o = new Object ();

label lb = o as Label;

if (lb = = null)
{
Response.Write ("type conversion failed");
}
Else
{
Response.Write ("type conversion succeeded");
}

In the above code, the CLR will only perform one type verification, which is more efficient than the IS

CLR via note 4.2 type conversion is and as difference

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.