Analysis of the difference between is and as in C #

Source: Internet
Author: User

This article mainly introduces the difference between is and as in C #, analyzes the principle and characteristics of IS and as as well as the usage difference, has a good learning value, the need for friends can refer to the following

This paper analyzes the difference between is and as in C #, and shares it for everyone's reference. The specific analysis is as follows:

I. Conversion of C # types

There are two types of conversions in C #: Explicit and implicit, with the following basic rules:

1. The base class object is converted to a subclass object, which must be explicitly converted to the rule: (type name) object.
2. Conversions of value types and reference types are either boxed (boxing) or unboxing (unboxing).
3. The subclass is transformed into a base class object.
4, the basic types of conversion between each other can be achieved by Covent Class.
5. The string type is converted to the corresponding base type using the Parse method, the parse method can be used in addition to the string type.
6, with GetType can obtain the exact type of object.
7. The subclass is transformed into a base class, using implicit conversion.

Second, is in C #

Checks whether an object is compatible with other specified types and returns a bool value that returns to True if an object is of a type or its parent type, otherwise it returns false. Never throw an exception
If the object reference is null, then the IS operator always returns false because no object can check its type.

For example

The code is as follows:

Object o = new Object ();
If (O is Label)
{
Label lb = (label) o;
Response.Write ("type conversion succeeded");
}
Else
{
Response.Write ("type conversion failed");
}

Iii. conversion rules for as in C #

1, check the compatibility of object type, and return the result of conversion, if not compatible, return null;
2, will not throw an exception;
3. If the result is null, then the forced type conversion throws a NullReferenceException exception;
4. When using as for type conversion, the object type to be converted must be the target type or the derived type of the conversion target type

For example

The code is as follows: Object o = new Object ();
label lb = o as Label;
if (lb = = null)
{
Response.Write ("type conversion failed");
}
Else
{
Response.Write ("type conversion succeeded");
}

There are several limitations to using the AS operator

The first is that there is no type conversion between types, which is written as a compilation error.

The code is as follows: NewType newvalue = new NewType ();
NewType1 newvalue = newvalue as NewType1;

The second is that it cannot be applied to the value type data, i.e. it cannot be written as follows (a compilation error will also occur).

The code is as follows: Object objtest = 11;
int nvalue = objtest as int;

Iv. the difference between as and is

1. As in the converted colleague and Judge compatibility, if the conversion cannot be done, then as returns Null (no new object is generated) instead of throwing an exception. With the as I think in the future will not use Try-catch to do type conversion judgment. Therefore, the as conversion succeeds to determine whether it is null.

2, as is a conversion or boxing conversion of a reference type type, and cannot be converted with a value type. If the value type can only be cast in conjunction with IS
3, is just do type-compatible judgment, do not perform a true type conversion. Returns TRUE or FALSE, does not return NULL, the object is null and returns FALSE.

4. As mode is more efficient than the IS mode, because the is for type conversion, you need to perform two type compatibility checks. As only one type-compatible, one null check is required, and a null check is faster than a type-compatible check.

Five, in the type conversion, you can follow the following way to choose

1. Object = = known reference type
Use the AS operator to complete

2. Object = = known value type
Use the IS operator first to judge, and then convert with a type-strong-turn method

3. Conversions between known reference types
You first need the corresponding type to provide the conversion function, and then use the type strong to convert

4. Conversion between known value types
It is best to use the static method involved in the system-provided convert class

The difference between six, (int) and Int32.Parse (), Convert.ToInt32 ()

1, (int) conversion: used when converting a type with a large range of values to a type with a small range of values, but if the converted value is greater than or less than the range of values, an incorrect result is obtained, and the conversion cannot be used to convert a string to int, which will cause an error.

2, Int32.Parse () conversion: used in a string-to-int type conversion that conforms to the number format, and can throw a corresponding exception on the wrong string number format.

3. Convert.ToInt32 () Conversion: Using this conversion, the supplied string must be a valid representation of the numeric value, and the number must not be an overflow. Otherwise throws an exception.

I hope this article is helpful to everyone's C # programming.

Analysis of the difference between is and as in C #

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.