28 Jan C# Type Casting
Type casting is to convert one type to another in C#. There are two types of casting in C#:
- Implicit Type Casting
- Explicit Type Casting
Implicit Type Casting
Implicit casting occurs automatically in C# and converts from smaller to larger types. Let us see an example wherein int is converted to double automatically:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
using System; namespace Demo { class Studyopedia { static void Main(string[] args) { int i = 20; /* This converts int to float i.e. implicit conversion, which is automatic */ float f = i; Console.WriteLine("Implicit Conversion..."); Console.WriteLine("int = {0}",i); Console.WriteLine("int converted to float = {0}", i); } } } |
Output
1 2 3 4 5 |
Implicit Conversion... int = 20 int converted to float = 20 |
Explicit Type Casting
The Explicit casting needs to be done manually in C# and converted from larger to smaller types. We use the cast operator for this. Let us first see the syntax for explicit type casting using the cast operator:
1 2 3 |
(type_name)value |
Let us follow the above syntax and see an example wherein double is converted to int manually using the cast operator:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
using System; namespace Demo { class Studyopedia { static void Main(string[] args) { float f = 5.75F; int i; /* This cast float to int i.e. explicit conversion, which is manual */ Console.WriteLine("Explicit Conversion..."); i = (int)f; Console.WriteLine("float = {0}",f); Console.WriteLine("float converted to int = {0}", i); } } } |
Output
1 2 3 4 5 |
Explicit Conversion... float = 5.75 float converted to int = 5 |
For this, some built-in methods are also provided by C#. The following are the methods:
- ToBoolean()
- ToByte()
- ToDateTime()
- ToDecimal()
- ToInt16()
- ToInt32()
- ToInt64()
- ToString()
Let us see them one by one with examples:
Convert.ToBoolean() Method in C#
To convert a specified type to Boolean type, use Convert.ToBoolean() method in C#. Let us see an example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
using System; namespace Demo { class Studyopedia { static void Main(string[] args) { float f = 5.75F; bool b; /* This cast float to bool i.e. explicit conversion, which is manual */ Console.WriteLine("Conversion to Boolean..."); b = Convert.ToBoolean(f); Console.WriteLine("float = {0}",f); Console.WriteLine("float converted to bool = {0}", b); } } } |
Output
1 2 3 4 5 |
Conversion to Boolean... float = 5.75 float converted to bool = True |
Convert.ToByte() Method in C#
To convert a specified type to Byte type, use the Convert.ToByte() method in C#. Let us see an example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
using System; namespace Demo { class Studyopedia { static void Main(string[] args) { float f = 25.75F; byte b; /* This cast float to byte i.e. explicit conversion, which is manual */ Console.WriteLine("Conversion to Byte..."); b = Convert.ToByte(f); Console.WriteLine("float = {0}",f); Console.WriteLine("float converted to byte = {0}", b); } } } |
Output
1 2 3 4 5 |
Conversion to Byte... float = 25.75 float converted to byte = 26 |
Convert.ToDateTime() Method in C#
To convert a specified type to DateTime type, use the Convert.ToDateTime() method in C#. Let us see an example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
using System; namespace Demo { class Studyopedia { static void Main(string[] args) { string dateStr = "01/07/2023"; DateTime dt; /* This cast string to DateTime i.e. explicit conversion, which is manual */ Console.WriteLine("Conversion to DateTime..."); dt = Convert.ToDateTime(dateStr); Console.WriteLine("String = {0}",dateStr); Console.WriteLine("String converted to DateTime = {0}", dt); } } } |
Output
1 2 3 4 5 |
Conversion to DateTime... String = 01/07/2023 String converted to DateTime = 1/7/2023 12:00:00 AM |
Convert.ToDecimal() Method in C#
To convert a specified type to Decimal type, use the Convert.ToDecimal() method in C#. Let us see an example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
using System; namespace Demo { class Studyopedia { static void Main(string[] args) { string str = "123.67"; decimal d; /* This cast string to Decimal i.e. explicit conversion, which is manual */ Console.WriteLine("Conversion to Decimal..."); d = System.Convert.ToDecimal(str); Console.WriteLine("String = {0}",str); Console.WriteLine("String converted to Decimal = {0}", d); } } } |
Output
1 2 3 4 5 |
Conversion to Decimal... String = 123.67 String converted to Decimal = 123.67 |
Convert.ToInt16() Method in C#
To convert a specified type to Int16 type, use the Convert.ToInt16() method in C#. Let us see an example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
using System; namespace Demo { class Studyopedia { static void Main(string[] args) { double d = 1.428; short s; /* This cast double to Int16 i.e. short i.e. explicit conversion, which is manual */ Console.WriteLine("Conversion to Int16..."); s = System.Convert.ToInt16(d); Console.WriteLine("Double = {0}",d); Console.WriteLine("Double to int16 i.e. short = {0}", s); } } } |
Output
1 2 3 4 5 |
Conversion to Int16... Double = 1.428 Double to int16 i.e. short = 1 |
Convert.ToInt32() Method in C#
To convert a specified type to Int32 type, use the Convert.ToInt32() method in C#. Let us see an example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
using System; namespace Demo { class Studyopedia { static void Main(string[] args) { double d = 18.29; int i; /* This cast double to Int32 i.e. int i.e. explicit conversion, which is manual */ Console.WriteLine("Conversion to Int32..."); i = System.Convert.ToInt32(d); Console.WriteLine("Double = {0}",d); Console.WriteLine("Double to int32 i.e. int = {0}", i); } } } |
Output
1 2 3 4 5 |
Conversion to Int32... Double = 18.29 Double to int32 i.e. int = 18 |
Convert.ToInt64() Method in C#
To convert a specified type to Int64 type, use the Convert.ToInt64() method in C#. Let us see an example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
using System; namespace Demo { class Studyopedia { static void Main(string[] args) { double d = 1690235.252; long l; /* This cast double to Int64 i.e. long i.e. explicit conversion, which is manual */ Console.WriteLine("Conversion to Int64..."); l = System.Convert.ToInt64(d); Console.WriteLine("Double = {0}",d); Console.WriteLine("Double to int64 i.e. long = {0}", l); } } } |
Output
1 2 3 4 5 |
Conversion to Int64... Double = 1690235.252 Double to int64 i.e. long = 1690235 |
Convert.ToString() Method in C#
To convert a specified type to string type, use the Convert.ToString() method in C#. Let us see an example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
using System; namespace Demo { class Studyopedia { static void Main(string[] args) { bool b = true; string str; /* This cast bool to string i.e. explicit conversion, which is manual */ Console.WriteLine("Conversion to String..."); str = System.Convert.ToString(b); Console.WriteLine("Bool = {0}",b); Console.WriteLine("Bool to String = {0}", str); } } } |
Output
1 2 3 4 5 |
Conversion to String... Bool = True Bool to String = True |
No Comments