A brief analysis of several forms of C data type conversion

  • 2020-05-17 06:16:17
  • OfStack

1, Convert. ToInt32 (); // converted to a 32-bit integer.
2. Variable.ToString(); / most commonly converted to a string.
3. The number after "order "+2514 // will be converted to a string.
4. ((class name A) object name X) // forcibly converts object X into object of class A.
5, int. Parse (string); Converts a string type to another type.
6. Also, if you want to convert to a reference type, you can use as
teacher tea = teahcer();
For example, student stu = tea as student;

(1) implicit conversion :1 is generally a low type to a high type conversion, to ensure that the value does not change.
Implicit value 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 char to ushort, int, uint, long, ulong, float, double or decimal.
From float to double.
There is no implicit conversion to type char, so the values of other integers are not automatically converted to type char.
Floating point cannot be implicitly converted to decimal
Implicit enumeration conversion
Implicit enumeration conversion allows you to convert the decimal integer 0 to any enumeration type.
Implicit reference conversion
Converts a derived class to a base class
An implicit reference conversion is a conversion between 1 class reference types that is always successful and therefore does not require any checking at run time.
The transformation using the
Boxed conversion allows the implicit conversion of value types to reference types.

(2) display conversion: also known as cast type conversion. The correctness of the data cannot be guaranteed.
(type)

(3) user-defined C# data type conversion
All user-defined transformations are static, using the static keyword
User-defined conversions are displayed and implicit and are declared with the implicit (implicit conversion) or explicit (display conversion) keywords.
static access trope conversion trope operator conversion type (parameter)
Example of C# data type conversion:


struct Number 
{ 
 private int value; 
 public Number(int value) { this.value=value; } 
 // User - defined integer to Number Implicit conversion of type  
 static public implicit operator Number(int value) { return new Number(value); } 
 // User defined from Number Type to integer display conversion  
 static public explicit operator int(Number n) { return n.value; } 
 // User defined from Number Type to string An implicit conversion of a type  
 static public implicit operator string(Number n) { return n.ToString(); } 
}  
class Test 
{ 
 static public void Main() 
 { 
  Number n; 
  n=10; 
  Console.WriteLine((int)n); 
  // Implicitly convert to string 
  Console.WriteLine(n); 
 } 
}

Using System Convert class
Converts one base data type to another.
Use the Parse method
Most predefined value types have this static method for converting the corresponding text to the corresponding value type.

Packing and unpacking
Boxing and unboxing enable value types to convert to and from object types.
Boxed conversion allows the implicit conversion of value type to reference type. Boxing the value of a value type consists of assigning an object instance and copying the value of a value type into that instance.

Example of C# data type conversion:
This example converts the integer variable i into an object o via boxing. This example shows that the object retains the original copy of the content, which is 123.

public static void Main() 
{ 
 int i=123; 
 object o=i;// Implicit boxing  
 i=456;// Change the variable i The value of the  
 Console.WriteLine("Thevalue-typevalue={0}",i);//456 
 Console.WriteLine("Theobject-typevalue={0}",o);//123 is i The duplicate values  
} 

Unboxed conversion: unboxed conversion allows you to explicitly convert a reference type to a value type.
Unboxing consists of two steps: first check that the object instance is a boxed value of a given value type, and then copy the value from the instance.
Example of C# data type conversion:
The following example illustrates the case of invalid unboxing, that is, how incorrect unboxing results in InvalidCastException. Using try and catch, an error message is displayed when an error occurs.

public class UnboxingTest 
{ 
 public static void Main() 
 { 
  int intI=123; 
  object o=intI;// packing  
  try 
  { // Cancellation of packing is invalid, short Not a boxed value type . Check to see if the object instance is a boxed value of a given value type 
   int intJ=(short)o; 
   Console.WriteLine("UnboxingOK."); 
  } 
  catch(InvalidCastException e) 
  { 
   Console.WriteLine("{0}Error:Incorrectunboxing.",e); 
  } 
 } 
}

Other transformations use operators
as
The as operator is used to perform an explicit type conversion of a reference type. If the type to be converted is compatible with the specified type, the conversion will succeed. If the type is not compatible, null is returned.
The expression as type
The as operator is similar to a type conversion, except that when the conversion fails, the as operator returns null instead of throwing an exception.
Example:

object o1= " SomeString " ; 
object o2=5; 
string s1=o1 as string;// Type is compatible s1= " SomeString "  
string s2=o2 as string;//s2=null

is
The is operator is used to check whether the type of the object is compatible with the given type (the object is or is derived from the type).
The expression is type
Example:

int i=10; 
if(iisobject)//true 
{}

sizeof
The sizeof operator is used to get the size (in bytes) of the value type.
sizeof(type)
The sizeof operator applies only to value types, not reference types.
The sizeof operator can only be used in unsafe mode.
Example:

unsafe 
{ 
 Console.WriteLine( " {0} " ,sizeof(int)); 
}


Related articles: