This article is a C # data type conversion of several forms of a detailed analysis of the introduction, the need for friends to reference the
1, Convert.ToInt32 ();///Convert to 32-bit integer.
2, variable. ToString ();/most commonly converted to strings. The number in the
3, order +2514//followed is converted to a string.
4, (class name a) object name X)//force object X to be converted to object A of Class A.
5, Int. Parse (string), converting a string to another type.
6, and also, if the type to be converted is a reference type, you can also use as
teacher tea = Teahcer ();
such as student Stu = tea as student;
(1) Implicit conversions: Typically, the low type converts to a high type to ensure that the value does not change.
Implicit numeric C # data type conversions:
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. The
does not exist an implicit conversion to a char type, so the value of other integers is not automatically converted to the Char type.
Floating-point types cannot be implicitly converted to decimal
Implicit enumeration conversions
Implicit enumeration conversions allow decimal integer 0 to be converted to any enumerated type.
Implicit reference conversion
derived classes convert to a base class
Implicit reference conversions are conversions between a class of reference types that can always succeed, and therefore do not require any checks at run time. The
boxing conversion
Boxing conversion allows a value type to be implicitly converted to a reference type.
(2) Display conversion: Also called coercion type conversion. The correctness of the data is not guaranteed.
(type) (expression)
(3) User-defined C # data type conversions
all user-defined conversions are static and use the Static keyword
User-defined conversion display and implicit, which are declared with implicit (implicit conversion) or explicit (display conversion) keywords.
Static access rhetorical character conversion rhetoric operator conversion type (parameters)
examples of C # data type conversions:
Copy Code code as follows:
struct number
{
private int value;
public number (int value) {This.value=value;}
//user-defined implicit conversion of integer to number type
static public implicit operator number (int value) {return new number (value);}
//user-defined display conversion from number to integer
static public Explicit operator int (number N) {return n.value;}
//user-defined implicit conversions from number type to string 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);
//implicit conversion to string
Console.WriteLine (n);
}
}
using the System.Convert class
converts a basic data type to another base data type.
using the Parse method
most predefined value types have this static method, which is used to convert the corresponding text to the corresponding value type.
Boxing and unboxing
Boxing and unboxing enable value types to be converted to and from object types.
Boxing conversions allow value types to be implicitly converted to reference types. Boxing a value of value type includes assigning an instance of an object and copying the value of the value type into the instance.
Examples of C # data type conversions:
This example converts the integer variable I to object o by boxing. This example shows that the object retains the original copy of the content, that is, 123.
Copy Code code as follows:
public static void Main ()
{
int i=123;
Object o=i;//An implicit boxing
i=456;//Change the value of variable i
Console.WriteLine ("Thevalue-typevalue={0}", i);//456
Console.WriteLine ("Theobject-typevalue={0}", O);//123 is the copy value of I
}
Unboxing conversions: unboxing conversions allow reference types to be explicitly converted to value types.
Unboxing includes the following two steps: First check that the object instance is a boxed value for a given value type, and then copy the value from the instance.
examples of C # data type conversions:
The following example illustrates the case of an invalid unboxing, that is, how an incorrect unboxing causes InvalidCastException. By using try and catch, an error message is displayed when an error occurs.
Copy Code code as follows:
public class Unboxingtest
{
public static void Main ()
{
int inti=123;
Object o=inti;//Boxing
Try
{//unboxing invalid, short is not a boxed value type. Check if the object instance is a boxed value for a given value type
int intj= (short) O;
Console.WriteLine ("Unboxingok.");
}
catch (InvalidCastException e)
{
Console.WriteLine ("{0}error:incorrectunboxing.", e);
}
}
}
Other conversions with 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 succeeds, or null if the type is incompatible.
expression as type
The as operator is similar to a type conversion, but when the conversion fails, the AS operator returns null instead of throwing an exception.
Example:
Copy Code code as follows:
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 type of the object is compatible with the given type (the object is that type or derived from that type).
expression is type
Example:
Copy Code code as follows:
int i=10;
if (iisobject)//true
{}
sizeof
The sizeof operator is used to obtain the size, in bytes, of a value type.
sizeof (type)
The sizeof operator applies only to value types, not to reference types.
The sizeof operator can only be used in unsafe mode.
Example:
Copy Code code as follows:
unsafe
{
Console.WriteLine ("{0}", sizeof (int));
}