4csharp Type Conversion PDF
4csharp Type Conversion PDF
T ype conversion is basically type casting or converting one type of data to another type. In C#, type casting has
two forms:
Implic it type c onversion - these conversions are performed by C# in a type-safe manner. Examples
are conversions from smaller to larg er integ ral types and conversions from derived classes to base
classes.
Explic it type c onversion - these conversions are done explicitly by users using the pre-defined
functions. Explicit conversions require a cast operator.
T he following example shows an explicit type conversion:
namespace TypeConversionApplication
{
class ExplicitConversion
{
static void Main(string[] args)
{
double d = 5673.74;
int i;
// cast double to int.
i = (int)d;
Console.WriteLine(i);
Console.ReadKey();
}
}
}
When the above code is compiled and executed, it produces the following result:
5673
S.N
T oBoolean
Converts a type to a Boolean value, where possible.
T oByte
Converts a type to a byte.
T oChar
Converts a type to a sing le Unicode character, where possible.
T oDateT ime
Converts a type (integ er or string type) to date-time structures.
T oDec imal
Converts a floating point or integ er type to a decimal type.
T oDouble
Converts a type to a double type.
T oInt16
T oInt32
Converts a type to a 32-bit integ er.
T oInt64
Converts a type to a 64-bit integ er.
10
T oSbyte
Converts a type to a sig ned byte type.
11
T oSing le
Converts a type to a small floating point number.
12
T oString
Converts a type to a string .
13
T oT ype
Converts a type to a specified type.
14
T oUInt16
Converts a type to an unsig ned int type.
15
T oUInt32
Converts a type to an unsig ned long type.
16
T oUInt64
Converts a type to an unsig ned big integ er.
When the above code is compiled and executed, it produces the following result:
75
53.005
2345.7652
True