Introduction to AS, is operators and coercion type conversions in C #

Source: Internet
Author: User

The biggest difference between as and cast is how to handle user-defined transformations. The operator as and is only checks the Run-time type of the object being converted, and does not perform other operations. If the run-time type of the converted object is neither the converted target type nor its derived type, then the transition will fail. But a forced transition uses the conversion operator to perform transformational operations, including any built-in numeric conversions (such as a long int).

In general, we should consider using as as a type conversion, and then consider using is, and finally consider using a cast.

If you use the as to transform data, it is not necessary to use is for testing. Just check that the return type is null.

Is and as as, are type conversions safe, they do not at any time in the conversion of the exception, so you can use is to safely determine the data type. When it is different from as as, the is just does type detection and returns logical values without conversion.

As cannot be used for value types, because value types cannot be null. In this case we should use coercion type conversion.

Eg: in the code

Double i =;  
Int j = i as int;  
Error 1 The AS operator must be used with a reference type or nullable type (' int ' is a non-nullable value type)

This paper url:http://www.bianceng.cn/programming/csharp/201410/45592.htm

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.