1. Convert.ToInt32(); //Convert to 32-bit integer.
2. Variable.ToString();/The most common conversion to strings.
3. "Order" + 2514 //The following numbers will be converted into strings.
4. ((Class name A) Object name X) //Forcibly convert object X into object A of class A.
5. (string); convert string types to other types.
6. Also, if the type you want to convert is a reference type, you can also use as
teacher tea = teahcer();
For example, student stu = tea as student;
(1) Implicit conversion: Generally, low-type conversion to high-type can ensure that the value does not change.
Implicit numerical 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 the char type, so the values of other integers are not automatically converted to the char type.
Floating point type cannot be implicitly converted to decimal type
Implicit enumeration conversion
Implicit enumeration conversion allows converting decimal integer 0 to any enum type.
Implicit reference conversion
Transformation of derived classes to base classes
Implicit reference conversion refers to conversions between a class of reference types, which can always succeed, so no checks are required at runtime.
Box Conversion
Boxing allows 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)(expression)
(3) User-defined C# data type conversion
All user-defined conversions are static, use the static keyword
User-defined conversions are divided into display and implicit, which are declared with the keyword implicit or explicit.
static access rhetorical symbol Convert rhetorical symbol operator Convert type (parameters)
C# data type conversion example:
struct Number
{
private int value;
public Number(int value) { =value; }
//User-defined implicit conversion from integer to Number
static public implicit operator Number(int value) { return new Number(value); }
//User customizes the display conversion from Number to integer
static public explicit operator int(Number n) { return ; }
//User customizes implicit conversion from Number type to string type
static public implicit operator string(Number n) { return (); }
}
class Test
{
static public void Main()
{
Number n;
n=10;
((int)n);
//Implicit conversion to string
(n);
}
}
Use class
Convert one primitive data type to another primitive data type.
Use the Parse method
Most predefined value types have this static method to convert the corresponding text into the corresponding value type.
Packing and unboxing
Boxing and unboxing enable value types to be converted to object types.
Boxing allows implicit conversion of "value type" to "reference type". The operation of boxing a value of "value type" includes assigning an instance of an object and copying the value of "value type" into that instance.
C# data type conversion example:
This example converts the integer variable i to an object o by boxing. This example shows that the object retains an original copy of the content, i.e. 123.
public static void Main()
{
int i=123;
object o=i;//Implicit packing
i=456;//Change the value of variable i
("Thevalue-typevalue={0}",i);//456
("Theobject-typevalue={0}",o);//123 is the copy value of i
}
Unboxing conversion:Unboxing allows explicit conversion of reference types to value types.
The unboxing operation consists of the following two steps: first check whether the object instance is a boxed value of a 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, i.e. how an incorrect unboxing causes an InvalidCastException. By 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
{ //Unboxing is invalid, short is not the value type that has been loaded. Check whether the object instance is a value that has been loaded for a given value type
int intJ=(short)o;
("UnboxingOK.");
}
catch(InvalidCastException e)
{
("{0}Error:Incorrectunboxing.",e);
}
}
}
Other conversion operators
as
The as operator is used to perform explicit type conversion of reference types. If the type to be converted is compatible with the specified type, the conversion will be successful; if the type is incompatible, null is returned.
Expression as type
The as operator is similar to 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 compatible s1=”SomeString”
string s2=o2 as string;//s2=null
is
The is operator is used to check whether the object's type is compatible with the given type (the object is or is derived from it).
Expression is type
Example:
int i=10;
if(iisobject)//true
{}
sizeof
The sizeof operator is used to obtain the size of the value type in bytes.
sizeof(type)
The sizeof operator applies only to value types, not to reference types.
The sizeof operator is only available in unsafe mode.
Example:
unsafe
{
(“{0}”,sizeof(int));
}