1. Sense of IS and

Source: Internet
Author: User

Http://space.itpub.net/12639172/viewspace-503457

1.

  • Any type can be safely converted to its base class type, which can be completed by implicit conversion;
  • Display conversion is required when any type is converted to its derived type. conversion rules are as follows: (type name) Object Name;
  • GetType can be used to obtain the exact type of any object;
  • You can use the covert class to convert basic types;
  • All types except string have the parse method, which is used to convert the string type to the corresponding basic type;
  • The conversion mechanism of value types and reference types is called boxing and unboxing ).
  • Is rules are as follows:

    • Check the compatibility of object types and return results, true or false;
    • No exception is thrown;
    • If the object is null, the return value is always false.

    Typical usage:

    1 object o = new object ();
    2
    3 Class
    4
    5 {
    6
    7}
    8
    9If (O is a) // perform the first type compatibility check
    10
    11 {
    12
    13 A = (a) O; // perform the second type compatibility check
    14
    15}
    16
    17

    The as rules are as follows:

    • Check the compatibility of object types and return results. If not, return null;
    • No exception is thrown;
    • If the result is null, The nullreferenceexception is thrown when the type conversion is forced.

    Typical usage:

    1 object o = new object ();
    2
    3 Class B
    4
    5 {
    6
    7}
    8
    9b B = O as B; // perform a type compatibility check.
    10
    11if (B! = NULL)
    12
    13 {
    14
    15 MessageBox. Show ("B is B's instance .");
    16
    17}
    18
    19

     

     

    2.

    Differences between C # is and

    There are two operators "is" and "as" to judge and convert types. The specific differences and usage are as follows:
    Is is in the judgment of the type. Returns true and false. If an object is of a certain type or its parent type, true is returned; otherwise, false is returned. In addition, the is operator will never throw an exception. The Code is as follows:

    System. boolean b1 = (O is system. Object); // B1 is true
    System. boolean b2 = (O is employee); // B2 is false

    If the object reference is null, the is Operator always returns false because no object can check its type, just like the following code.

    If (O is employee ){
    Employee E = (employee) O;
    // Use e in the IF statement
    }

    In the code above, CLR checks the object type twice: The is operation first checks whether the object referenced by O is compatible with the employee type. If it is compatible, the CLR in the IF statement checks whether o is referenced by an employee when executing the conversion. This programming paradigm is very common. C # provides a new type check and conversion method. That is, the as operator can improve the performance while simplifying the code. The Code is as follows:

    Employee E = O as employee;
    If (E! = NULL)
    {
    // Use e in the IF statement
    }

    This as operation improves the performance even if it is equivalent to the above Code and performs only one type check at the same time. If the types are the same, a non-null reference is returned. Otherwise, an empty reference is returned.

    3 ..Differences between (INT), int32.parse () and convert. toint32 () in C #

    The Int keyword indicates an integer that is 32-bit. Its. NET Framework type is system. int32.

    (INT) indicates explicit forced conversion, which is a type conversion. Implicit conversions can be used when we convert from int type to long, float, double, or decimal type. However, explicit forced conversions are required when we convert from long type to int type, otherwise, a compilation error occurs.

    Int32.parse () indicates converting the string of a number to a 32-bit signed integer.
    A common method is public static int parse (string ).
    If the string is null, an argumentnullexception exception is thrown;
    If the string format is incorrect, A formatexception is thrown;
    If the string value is smaller than minvalue or a number greater than maxvalue, an overflowexception exception is thrown.

    Convert. toint32 () can convert values of multiple types (including object reference types) to int type, because it has many overloaded versions [2]:
    Public static int toint32 (object );
    Public static int toint32 (bool );
    Public static int toint32 (byte );
    Public static int toint32 (char );
    Public static int toint32 (decimal );
    Public static int toint32 (double );
    Public static int toint32 (short );
    Public static int toint32 (long );
    Public static int toint32 (sbyte );
    Public static int toint32 (string );

    The applications of (INT), int32.parse (), and convert. toint32 () are as follows:

    Example 1:

    Long longtypes = 100;
    Int inttype = longtype; // error. Explicit forced conversion is required.
    Int inttype = (INT) longtype; // The value is correct. An explicit forced conversion is used.

    Example 2:

    String stringtype = "12345 ";
    Int inttype = (INT) stringtype; // error. The string type cannot be directly converted to the int type.
    Int inttype = int32.parse (stringtype); // correct

    Example 3:

    Long longtypes = 100;
    String stringtype = "12345 ";
    Objecttype = "54321 ";
    Int inttype = convert. toint32 (longtype); // correct
    Int inttype = convert. toint32 (stringtype); // correct
    Int inttype = convert. toint32 (objecttype); // correct

    Example 4:

    Double doubletype = int32.maxvalue + 1.011;
    Int inttype = (INT) doubletype; // although the operation is correct, an error is returned.
    Int inttype = convert. toint32 (doubletype) // throw an overflowexception exception

    The differences between (INT), int32.parse (), and convert. toint32 () are as follows:

    The first one is used for explicit forced conversions from the long type or floating point type to the int type. However, if the converted value is greater than int32.maxvalue or less than int32.minvalue, an error is returned.

    The second one is used during the conversion from string to int type that conform to the numeric format, and an exception can be thrown to the wrong string numeric format.

    Third, you can convert multiple types of values to the int type, or throw an exception to the wrong value.

    Regardless of the type of numerical conversion, the accuracy of numerical values must be considered.

    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.