Analysis of the difference between is and as in C

  • 2020-11-18 06:25:09
  • OfStack

This paper analyzes the differences between is and as in C# and shares them with you for your reference. The specific analysis is as follows:

1. C# conversion

There are two types of conversions in c# : explicit and implicit. The basic rules are as follows:

1. The base class object is transformed into a subclass object, which must be explicitly transformed. Rules :(type name) object.
2. The conversion of value types and reference types is encasement (boxing) or unpacking (unboxing).
3. Subclasses are converted to base class objects.
4. Basic types can be converted to each other using Covent class.
5. Parse method is used to convert string types to corresponding basic types. Parse method can be used for all types except String type.
6. The exact type of object can be obtained with GetType.
7, the subclass into the base class, using implicit conversion.

2. is in C#

Checks whether an object is compatible with other specified types and returns an Bool value, true if an object is of a type or its parent, or false if not. An exception is never thrown
If the object reference is null, the is operator always returns false, because there is no object to check its type.

For example,

object o = new object();
if (o is Label)
{
    Label lb = (Label)o;
    Response.Write(" Type conversion successful ");
}
else
{
    Response.Write(" Type conversion failed ");  
}

3. Conversion rules for as in C#

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

For example,

object o = new object();    
Label lb = o as Label;   
if (lb == null)
{
    Response.Write(" Type conversion failed ");
}
else
{     
    Response.Write(" Type conversion successful ");
}

There are several limitations to using the as operator

The first is that there is no type conversion between types, which means the following code will cause a compilation error.

NewType newValue = new NewType();
NewType1 newValue = newValue as NewType1;

The second is that it cannot be applied to value type data, i.e., it cannot be written as follows (compile errors also occur).

object objTest = 11;
int nValue = objTest as int;

4. Differences between as and is

1. AS's colleagues in the transformation also judge the compatibility. If the transformation cannot be carried out, as returns null (no new object is generated) instead of throwing an exception. With AS I think we should stop using ES83en-ES84en for type conversions. Therefore, as conversion is successful to determine whether it is null.

2. AS is a conversion of reference type type or boxing type, and cannot be used with value type conversion. If it is a value type, only is can be used to cast
3. IS only makes type compatibility judgment, and does not perform real type conversion. Returns true or false, does not return null, object null also returns false.

4. The efficiency of AS mode is higher than that of IS mode, because two type compatibility checks need to be performed for type conversion with the help of IS. AS, on the other hand, needs only one type compatibility check and one null check, which is faster than the type compatibility check.

5. When doing type conversion, you can choose as follows

1, Object = > Known reference type
Use the as operator to do this

2, Object = > Known value type
The is operator is used to make the judgment, and then the conversion is performed with type strong transformation

3. Conversion between known reference types
First, you need to provide a conversion function for the corresponding type, and then use a strong conversion mode for conversion

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

6. The difference between (int) and Int32.ES131en (), Convert.ES133en32 ()3

1. (int) conversion: used when converting a type with a large numerical range to a type with a small numerical range. However, if the converted value is greater than or less than the numerical range, an error result will be obtained.

2. Int32.Parse () conversion: used in the conversion of string to int types that conform to the numeric format and can throw corresponding exceptions to the wrong string numeric format.

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

Hopefully this article has helped you with your C# programming.


Related articles: