There are two types of conversions in C #: explicit and implicit. The basic rules are as follows.
- The base class object must be converted to a subclass object. The rule is: (type name) object.
- The value type and reference type are converted using boxing or unboxing ).
- The subclass is converted to the base class object.
- Conversion between basic types can be implemented using the convert class.
- The parse method is used to convert the string type to the corresponding basic type. The parse method can be used for other types except the string type.
- You can use GetType to obtain the exact type of an object.
- The subclass is converted to the base class by implicit conversion.
The AS and is operators are described below.
- As: used for conversion between compatible reference types.
- Is: Check whether the object is compatible with the given type.
- If the conversion fails, as returns NULL (no new object is generated) instead of an exception. With as, I don't want to use try-catch to judge type conversion in the future. Therefore, if the as conversion is successful, you must determine whether it is null.
- As is a conversion or packing conversion of the reference type, and cannot be converted to the value type. If it is a value type, it can only be combined with is for forced conversion.
object objTest = 11; if( objTest is int ) { int nValue = (int )objTest; }
- Is is only used to judge the type compatibility and does not execute real type conversion. Returns true or false. No null is returned. If the object is null, false is returned.
- The as mode is more efficient than the is mode, because the type conversion with is requires two type compatibility checks. As only requires one type compatibility and one null check, and the null check is faster than the type compatibility check.
In data type conversion, C # is stricter than other languages, and explicit data conversion is required.
For ease of operation, C # also provides an is Operator for conversion, which automatically checks whether the current situation is compatible with the type and returns the result. And it does not throw an exception. If the object reference is null, the is always returns false.
if (cls1 is Class2) { Class2 cls2 = (Class2)cls1; } else System.Console.WriteLine("Error 2!");
I usually use this method for type conversion. However, I read an article today and compared it with another method. The as operator is used for conversion, to know that as can slightly improve the performance than is.
Class2 cls2 = cls1 as Class2; if (cls2!=null) System.Console.WriteLine("Ok"); else System.Console.WriteLine("Error!");
As is slightly different. It checks whether the referenced object is compatible. If it is not compatible, null is returned. Therefore, null is required.
Comparing the two methods, is requires two object type checks, while as requires an object type check and a null check. The null check overhead is less than the object type check. It is more efficient than the as method.
Usage of question marks (non-expressions) in C #: single question mark and double question mark.
From: http://www.cnblogs.com/zjp8023/archive/2008/11/25/ASIS.html