What are the conversions between data types in C# programming language?
(1) Implicit conversion: generally, a low type is converted into a high type, which can ensure that the value will not change. Implicit numeric C# data type conversion: from sbyte to short, int, long, float, double or decimal. From byte to short, ushort, int, uint, long, ulong, float double or decimal. From short to int, long, float, double or decimal. From ushort to int, uint, long, ulong, float, double or decimal. From int to long, float, double or decimal. From uint to long, ulong, float, double or decimal. From long to float, double or decimal. From ulong to float, double or decimal. From floating point to double precision. There is no implicit conversion to char type, so the values of other integers are not automatically converted to char type. Floating-point type cannot be implicitly converted to decimal type Implicit enumeration conversion Implicit enumeration conversion allows decimal integer 0 to be converted to any enumeration type. Implicit reference conversion from derived class to base class Implicit reference conversion refers to the conversion between a class of reference types, which can always succeed, so there is no need to check it at runtime. Box Conversion Box Conversion allows implicit conversion of value types to reference types. (2) Display conversion: also called forced type conversion. The correctness of the data cannot be guaranteed. (Type) (Expression) (3) Custom C# data type conversion All custom conversions are static. User-defined transformations should be displayed and implied with the static keyword, and they are declared with the implicit (implicit transformation) or explicit (explicit transformation) keyword. Static access rhetoric conversion rhetoric operator conversion type (parameter) C# data type conversion example: using the system; Structure number {private int value WeChat official account (int value) {this.value = value; }//User-defined implicit conversion from integer to number Static public implicit operator Number(int value){ Returns a new number (value); }//The static public explicit operator int ($ NUMBER n) {returnn. value; }//User-defined implicit conversion from numeric type to string type Static public implicit operator string (NUMBER n) {returnn.tostring (); } } class Test { static public void Main(){ Number n; n = 10; Console. WriteLine((int)n); //Implicit conversion to the string console. Write line (n); }} Use the system. Conversion classes convert one basic data type to another. Use the Parse method. Most predefined value types have this static method to convert the corresponding text into the corresponding value type. Boxing and unboxing boxing and unboxing enable value types to be converted to and from object types. The boxing conversion allows implicit conversion of a "value type" to a "reference type". Boxing the value of a value type includes allocating an object instance and copying the value of the value type to that instance. C# data type conversion example: This example converts integer variable I into object O through boxing. This example shows that the object retains the original copy of the content, that is, 123. Use the system; class test boxing { public static void Main(){ int I = 123; Object o = I;; //Implicit package i = 456// Change the value of variable isconsole. writeline ("the value-type value = {0}", i); //456 console. WriteLine(" the object-type value = { 0 } ",o); // 123 is the copied value of i}} unboxing conversion: unboxing conversion allows explicit conversion of reference types to value types. The unpacking operation includes the following two steps: first, check whether the object instance is a boxed value of the given value type, and then copy the value from the instance. C# data type conversion example: The following example illustrates the case of invalid unboxing, that is, how the wrong unboxing leads to InvalidCastException. By using try and catch, an error message is displayed when an error occurs. Use the system; public class unboxing test { public static void Main(){ int intI = 123; Object o = inti//boxing try {//boxing is invalid, and short is not a boxed value type. Check whether the object instance is a boxed value of the given value type int intJ=(short)o; Console. WriteLine(" unbox ok。" ); } catch(InvalidCastException e){ Console。 WriteLine(“{ 0} Error: Incorrect unboxing. ,e); }}} Other conversion operators as as operators are used to perform explicit type conversion of reference types. If the type to be converted is compatible with the specified type, the conversion will succeed; If the types are incompatible, null is returned. The expression as type as operator is similar to type conversion, except that when the conversion fails, the as operator will return null instead of throwing an exception. For example: object o1= "something"; Object O2 = 5;; string s 1 = o 1 as string; //type compatibility s1= "something" string2 = O2 as string; //s2=null is is operator is used to check whether the type of an object is compatible with a given type (whether the object is of this type or derived from this type). Examples of expressions are type: int I =10; The if (iisobject)//true {} sizeof operator is used to get the size (in bytes) of the value type. The sizeof(type) sizeof operator only applies to value types, not to reference types. The sizeof operator can only be used in unsafe mode.