Type conversion is converting one type of data to another type. It is also known as Type Casting. In C#, type casting has two forms
The following example shows an explicit type conversion −
using System;
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);
}
}
}
When the above code is compiled and executed, it produces the following result −
5673
C# provides the following are most important built-in type conversion methods −
The following example converts various value types to string type −
using System; 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 are given a character called ch, print the ASCII value of the character.
Example Input:
ch = 'a'
Example Output:
97