C # Type Conversions
Type conversions are essentially type casting, or the conversion of data from one type to another. There are two forms of type casting in C #:
Implicit type conversions-these transformations are the default in C # conversions that are made in a safe manner. For example, a small integer type is converted to a large integer type, from a derived class to a base class.
Explicit type conversions-these transformations are performed explicitly by the user using predefined functions. An explicit conversion requires a cast operator.
The following example shows an explicit type conversion:
Namespace typeconversionapplication{ class explicitconversion { static void Main (string[] args) { double d = 5673.74; int i; Casts a double to int i = (int) d; Console.WriteLine (i); Console.readkey ();}}}
When the above code is compiled and executed, it produces the following results:
5673
C # Type Conversion method
C # provides the following built-in type conversion methods:
Serial number
Method & Description
1 ToBoolean
If possible, convert the type to a Boolean type.
2 ToByte
Converts the type to a byte type.
3 ToChar
If possible, convert the type to a single Unicode character type.
4 ToDateTime
Converts a type (integer or string type) to a date-time structure.
5 ToDecimal
Converts a floating-point or integer type to a decimal type.
6 ToDouble
Converts the type to a double-precision floating-point type.
7 ToInt16
Converts the type to a 16-bit integer type.
8 ToInt32
Converts the type to a 32-bit integer type.
9 ToInt64
Converts the type to a 64-bit integer type.
Ten ToSByte
Converts the type to a signed byte type.
ToSingle
Converts the type to a small floating-point number type.
ToString
Converts the type to a string type.
ToType
Converts the type to the specified type.
ToUInt16
Converts the type to a 16-bit unsigned integer type.
ToUInt32
Converts the type to a 32-bit unsigned integer type.
ToUInt64
Converts the type to a 64-bit unsigned integer type.
The following example converts the type of a different value to a string type:
Namespace typeconversionapplication{ class stringconversion { static void Main (string[] args) { int i =; float f = 53.005f; Double d = 2345.7652; BOOL B = true; Console.WriteLine (i.ToString ()); Console.WriteLine (F.tostring ()); Console.WriteLine (D.tostring ()); Console.WriteLine (B.tostring ()); Console.readkey ();}}}
When the above code is compiled and executed, it produces the following results:
7553.0052345.7652True
The above is the "C # Tutorial" C # type conversion content, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!