C # is the difference between analysis and as the

  1. Type Conversion
    • Any type may be converted to its base type, with the implicit conversion is completed;
    • Any type into its derived types must be explicit conversion. As :( type name) object name;
    • Use of any type GetType can obtain accurate object;
    • Convert class basic types can be used to achieve the type of conversion;
    • In addition to other types of string has Parse method for converting a string type substantially corresponding to the type;
    • Value and reference type of conversion is called boxing (Boxing) or unpacking (Unboxing);
  2. as / is exemplary
    • It is conversion rules:
      • Checking compatibility object type, and returns the result true (false);
      • We will not throw an exception;
      • If the object is null, just return false;
        Example:
        Copy the code
        Object = O. 1 "ABC";   
         2 IF (O IS String) // performing a first type compatibility checking   
         . 3 {   
         . 4 S = String (String) O; // perform a second type of compatibility checking, and convert   
         5 MessageBox.Show ( "conversion successful!");   
         . 6}   
         . 7 the else   
         . 8 {   
         . 9 MessageBox.Show ( "conversion failed!");   
        10}  
        Copy the code
    • as conversion rules:
      • Checking compatibility object type, and returns the transformed result, if incompatible return null; 
      • We will not throw an exception;
      • If the determination result is empty, then the enforcement type conversion throws NullReferenceException exception; Example:
        Copy the code
        Object = O. 1 "ABC";   
        2 AS String String S = O; // performing a first type compatibility check, and returns the result   
        . 3 IF (! = Null S)    
        . 4 MessageBox.Show ( "conversion successful!");   
        the else 5   
        6 MessageBox.Show ( "conversion failed!");  
        Copy the code

        as is less than perform a compatibility check, performance may be a little higher.

Guess you like

Origin www.cnblogs.com/LiTZen/p/12120790.html