SoFunction
Updated on 2025-03-06

Analysis of the difference between is and as in C#

This article analyzes the difference between is and as in C# and shares it with you for your reference. The specific analysis is as follows:

1. Conversion of C# type

In C#, type conversion is divided into two types: explicit and implicit. The basic rules are as follows:

1. When the base class object is converted into a subclass object, it must be converted explicitly. Rules: (type name) object.
2. The conversion of value types and reference types is boxing or unboxing.
3. Subclasses are converted into base class objects.
4. The conversion of basic types between each other can be achieved by using the Covent class.
5. Use the Parse method to convert the string type to the corresponding basic type. Except for the String type, you can use the Parse method.
6. Use GetType to get the exact type of the object.
7. Subclasses are converted into base classes and implicit conversion is adopted.

2. Is in C#

Check whether an object is compatible with other specified types and returns a Bool value. If an object is of a certain type or its parent type, it will return true, otherwise it will return false. Exception will never be thrown
If the object reference is null, the is operator always returns false because no object can check its type.

For example

Copy the codeThe code is as follows:
object o = new object();
if (o is Label)
{
    Label lb = (Label)o;
("Type conversion succeeded");
}
else
{
("Type conversion failed");
}

3. The conversion rules of as in C#

1. Check the compatibility of the object type and return the conversion result. If it is incompatible, return null;
2. No exception is thrown;
3. If the result is judged to be empty, enforcing the type conversion will throw a NullReferenceException exception;
4. When using as to type conversion, the object type to be converted must be the target type or the derived type of the converted target type.

For example

Copy the codeThe code is as follows:
object o = new object();   
Label lb = o as Label;   
if (lb == null)
{
("Type conversion failed");
}
else
{     
("Type conversion succeeded");
}

There are several limitations to using the as operator

The first is that you don’t need to convert type between types, that is, you will have a compilation error if you write it as follows.

Copy the codeThe code is as follows:
NewType newValue = new NewType();
NewType1 newValue = newValue as NewType1;

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

Copy the codeThe code is as follows:
object objTest = 11;
int nValue = objTest as int;

4. The difference between as and is

1. AS colleagues in the conversion also judge compatibility. If the conversion cannot be performed, as will return null (no new object was generated) instead of throwing an exception. With AS, I think I will not use try-catch to make type conversion judgments in the future. Therefore, it is necessary to determine whether the as conversion is null.

2. AS is a conversion of reference type or boxing conversion, and cannot be converted with value type. If it is a value type, it can only be cast in combination with is
3. IS only makes type compatibility judgments and does not perform real type conversion. Returns true or false, no null is returned, and the object is null will also return false.

4. The efficiency of AS mode is higher than that of IS mode, because two type compatibility checks are required for type conversion with the help of IS. AS only needs to do type compatibility once, and a null check, which is faster than type compatibility check.

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

1. Object => Known reference type
Use the as operator to complete

2. Object => Known value type
First use the is operator to make judgments, and then use type strong conversion method to convert

3. Conversion between known reference types
First, the corresponding type needs to provide a conversion function, and then use type strong conversion method to convert

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

6. The difference between (int) and (), Convert.ToInt32()

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

2. () Conversion: Used in the process of converting string to int type that conforms to the number format, and can throw corresponding exceptions to the wrong string numeric format.

3. Convert.ToInt32() conversion: Using this conversion, the provided string must be an effective expression of the numeric value, and the number must not be an overflowing number. Otherwise, an exception is thrown.

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