• Home
  •  Casting and Type Conversions

 Casting and Type Conversions

Agenda

Casting and Type Conversions in C#

Type Conversion Methods in C#

int i = 35;

double d = 85.645;

float f = 30.78F;

Console.WriteLine(Convert.ToString(f));

Console.WriteLine(Convert.ToUInt32(f));

Console.WriteLine(Convert.ToInt64(f));

Console.WriteLine(Convert.ToString(d));

Console.WriteLine(Convert.ToUInt32(d));

Console.WriteLine(Convert.ToInt16(d));

Console.WriteLine(Convert.ToInt32(d));

Console.WriteLine(Convert.ToInt64(d));

Console.WriteLine(Convert.ToDouble(i));

Console.WriteLine(Convert.ToDecimal(i));

MethodDescription
ToBollenIt will converts a type to Bollen value
ToCharIt will converts a type to a charecter value
ToByteIt will converts a value to Byte value
ToDecimalIt will converts a value to decimal point value
ToDoubleIt will converts a type to double data type
ToInt16It will converts a type to 16-bit integer
ToInt32It will converts a type to 32-bit integer
ToInt64It will converts a type to 64-bit integer
ToStringIt will converts a 54swz given type to String
ToUlnt16It will converts a type to unsigned 16-bit integer
ToUlnt32It will converts a type to unsigned 32-bit integer


int i = 55;
double d = 78.523;
float f = 89.78F;
Console.WriteLine(Convert.ToUInt16(f));
Console.WriteLine(Convert.ToUInt32(f));
Console.WriteLine(Convert.ToUInt64(f));
Console.WriteLine(Convert.ToUInt16(d));
Console.WriteLine(Convert.ToUInt32(d));
Console.WriteLine(Convert.ToUInt64(d));
Console.WriteLine("*****");

Console.WriteLine(Convert.ToInt16(f));
Console.WriteLine(Convert.ToInt32(f));
Console.WriteLine(Convert.ToInt64(f));
Console.WriteLine(Convert.ToInt16(d));
Console.WriteLine(Convert.ToInt32(d));
Console.WriteLine(Convert.ToInt64(d));

Console.WriteLine(Convert.ToString(f));
Console.WriteLine(Convert.ToString(d));
Console.WriteLine(Convert.ToString(i));

double myDouble1 = 56.123;
int myInt2 = (int)myDouble1;
Byte myByte1 = (Byte)myDouble1;