C # type conversion, is, as, single question mark, double question mark decryption

Source: Internet
Author: User
There are two types of conversions in C #: explicit and implicit. The basic rules are as follows.
  1. The base class object must be converted to a subclass object. The rule is: (type name) object.
  2. The value type and reference type are converted using boxing or unboxing ).
  3. The subclass is converted to the base class object.
  4. Conversion between basic types can be implemented using the convert class.
  5. 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.
  6. You can use GetType to obtain the exact type of an object.
  7. 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.
  1. 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.
  2. 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; }
  3. 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.
  4. 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.
  • Single question mark: it is used to assign null values to the variable (INT type) when setting the initial value, rather than 0.
  • Double question mark: used to determine and assign values. First, judge whether the current variable is null. If yes, you can assign a new value. Otherwise, Skip.
    Public Int? Para; // public int para; if this line is not commented out and the previous line is commented out, an error will be reported below! Public int PAR () {return this. Para ?? 0 ;}

From: http://www.cnblogs.com/zjp8023/archive/2008/11/25/ASIS.html

Related Article

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.