0% found this document useful (0 votes)
63 views

4csharp Type Conversion PDF

Uploaded by

Abhisek Mohanty
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
63 views

4csharp Type Conversion PDF

Uploaded by

Abhisek Mohanty
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

C# - TYPE CONVERSION

http://www.tuto rialspo int.co m/csharp/csharp_type _co nve rsio n.htm

Co pyrig ht tuto rials po int.co m

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

C# Type Conversion Methods


C# provides the following built-in type conversion methods:

S.N

Methods & Desc ription

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

Converts a type to a 16-bit integ er.


8

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.

T he following example converts various value types to string type:


namespace TypeConversionApplication
{
class StringConversion
{
static void Main(string[] args)
{
int i = 75;
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 result:
75
53.005
2345.7652
True

You might also like