Type conversion refers to the process of converting a value from one data type to another. There are two main types of type conversions in C#: implicit and explicit conversions.
1. Implicit Type Conversion
Implicit type conversion is automatically performed by the compiler when it is safe to do so. This typically occurs when converting from a smaller data type to a larger data type (e.g., from int
to long
, or from float
to double
).
Example of Implicit Conversion:
csharp1using System; 2 3class Program 4{ 5 static void Main() 6 { 7 int intValue = 123; 8 long longValue = intValue; // Implicit conversion from int to long 9 double doubleValue = intValue; // Implicit conversion from int to double 10 11 Console.WriteLine("Integer Value: " + intValue); 12 Console.WriteLine("Long Value: " + longValue); 13 Console.WriteLine("Double Value: " + doubleValue); 14 } 15}
2. Explicit Type Conversion (Casting)
Explicit type conversion is required when converting from a larger data type to a smaller data type or when converting between incompatible types. This is done using casting.
Example of Explicit Conversion:
csharp1using System; 2 3class Program 4{ 5 static void Main() 6 { 7 double doubleValue = 123.45; 8 int intValue = (int)doubleValue; // Explicit conversion from double to int 9 Console.WriteLine("Double Value: " + doubleValue); 10 Console.WriteLine("Converted Integer Value: " + intValue); 11 } 12}
Note: When converting from double
to int
, the fractional part is truncated.
3. Using Convert
Class
C# provides a Convert
class that can be used for type conversion. This class provides methods for converting between different base types.
Example of Using Convert
Class:
csharp1using System; 2 3class Program 4{ 5 static void Main() 6 { 7 string stringValue = "123"; 8 int intValue = Convert.ToInt32(stringValue); // Convert string to int 9 double doubleValue = Convert.ToDouble(stringValue); // Convert string to double 10 11 Console.WriteLine("String Value: " + stringValue); 12 Console.WriteLine("Converted Integer Value: " + intValue); 13 Console.WriteLine("Converted Double Value: " + doubleValue); 14 } 15}
4. TryParse Method
For converting strings to numeric types, C# provides the TryParse
method, which attempts to parse a string and returns a boolean indicating success or failure.
Example of TryParse
:
csharp1using System; 2 3class Program 4{ 5 static void Main() 6 { 7 string stringValue = "456"; 8 int intValue; 9 10 bool success = int.TryParse(stringValue, out intValue); 11 if (success) 12 { 13 Console.WriteLine("Parsed Integer Value: " + intValue); 14 } 15 else 16 { 17 Console.WriteLine("Failed to parse the string to an integer."); 18 } 19 } 20}
5. Boxing and Unboxing
Boxing is the process of converting a value type to an object type, while unboxing is the reverse process.
Example of Boxing and Unboxing:
csharp1using System; 2 3class Program 4{ 5 static void Main() 6 { 7 int intValue = 100; 8 // Boxing 9 object boxedValue = intValue; // int is boxed into an object 10 11 // Unboxing 12 int unboxedValue = (int)boxedValue; // Unboxing back to int 13 14 Console.WriteLine("Boxed Value: " + boxedValue); 15 Console.WriteLine("Unboxed Value: " + unboxedValue); 16 } 17}
Summary
- Implicit Conversion: Automatically performed by the compiler when safe.
- Explicit Conversion: Requires casting when converting between incompatible types or from larger to smaller types.
- Convert Class: Provides methods for converting between types.
- TryParse Method: Safely parses strings to numeric types without throwing exceptions.
- Boxing and Unboxing: Converting value types to reference types and back.
C# Type Conversion Methods :
Sr.No. | Method | Description |
---|---|---|
1 | ToBoolean | Converts a type to a Boolean value, where possible. |
2 | ToByte | Converts a type to a byte. |
3 | ToChar | Converts a type to a single Unicode character, where possible. |
4 | ToDateTime | Converts a type (integer or string type) to date-time structures. |
5 | ToDecimal | Converts a floating point or integer type to a decimal type. |
6 | ToDouble | Converts a type to a double type. |
7 | ToInt16 | Converts a type to a 16-bit integer. |
8 | ToInt32 | Converts a type to a 32-bit integer. |
9 | ToInt64 | Converts a type to a 64-bit integer. |
10 | ToSByte | Converts a type to a signed byte type. |
11 | ToSingle | Converts a type to a small floating point number. |
12 | ToString | Converts a type to a string. |
13 | ToType | Converts a type to a specified type. |
14 | ToUInt16 | Converts a type to an unsigned 16-bit integer. |
15 | ToUInt32 | Converts a type to an unsigned 32-bit integer. |
16 | ToUInt64 | Converts a type to an unsigned 64-bit integer. |
Example Usage for Each Method
1. ToBoolean
csharp1using System; 2 3class Program 4{ 5 static void Main() 6 { 7 object obj = 1; 8 bool boolValue = Convert.ToBoolean(obj); 9 Console.WriteLine($"ToBoolean: {boolValue}"); // Output: True 10 } 11}
2. ToByte
csharp1using System; 2 3class Program 4{ 5 static void Main() 6 { 7 object obj = 255; 8 byte byteValue = Convert.ToByte(obj); 9 Console.WriteLine($"ToByte: {byteValue}"); // Output: 255 10 } 11}
3. ToChar
csharp1using System; 2 3class Program 4{ 5 static void Main() 6 { 7 object obj = 'A'; 8 char charValue = Convert.ToChar(obj); 9 Console.WriteLine($"ToChar: {charValue}"); // Output: A 10 } 11}
4. ToDateTime
csharp1using System; 2 3class Program 4{ 5 static void Main() 6 { 7 object obj = "2023-10-05"; 8 DateTime dateTimeValue = Convert.ToDateTime(obj); 9 Console.WriteLine($"ToDateTime: {dateTimeValue}"); // Output: 10/5/2023 12:00:00 AM 10 } 11}
5. ToDecimal
csharp1using System; 2 3class Program 4{ 5 static void Main() 6 { 7 object obj = 123.45; 8 decimal decimalValue = Convert.ToDecimal(obj); 9 Console.WriteLine($"ToDecimal: {decimalValue}"); // Output: 123.45 10 } 11}
6. ToDouble
csharp1using System; 2 3class Program 4{ 5 static void Main() 6 { 7 object obj = 123; 8 double doubleValue = Convert.ToDouble(obj); 9 Console.WriteLine($"ToDouble: {doubleValue}"); // Output: 123 10 } 11}
7. ToInt16
csharp1using System; 2 3class Program 4{ 5 static void Main() 6 { 7 object obj = 32767; 8 short int16Value = Convert.ToInt16(obj); 9 Console.WriteLine($"ToInt16: {int16Value}"); // Output: 32767 10 } 11}
8. ToInt32
csharp1using System; 2 3class Program 4{ 5 static void Main() 6 { 7 object obj = 123456; 8 int int32Value = Convert.ToInt32(obj); 9 Console.WriteLine($"ToInt32: {int32Value}"); // Output: 123456 10 } 11}
9. ToInt64
csharp1using System; 2 3class Program 4{ 5 static void Main() 6 { 7 object obj = 9223372036854775807; // Max value for Int64 8 long int64Value = Convert.ToInt64(obj); 9 Console.WriteLine($"ToInt64: {int64Value}"); // Output: 9223372036854775807 10 } 11}
10. ToSByte
csharp1using System; 2 3class Program 4{ 5 static void Main() 6 { 7 object obj = -128; 8 sbyte sbyteValue = Convert.ToSByte(obj); 9 Console.WriteLine($"ToSByte: {sbyteValue}"); // Output: -128 10 } 11}
11. ToSingle
csharp1using System; 2 3class Program 4{ 5 static void Main() 6 { 7 object obj = 123.45; 8 float singleValue = Convert.ToSingle(obj); 9 Console.WriteLine($"ToSingle: {singleValue}"); // Output: 123.45 10 } 11}
12. ToString
csharp1using System; 2 3class Program 4{ 5 static void Main() 6 { 7 object obj = 123; 8 string stringValue = Convert.ToString(obj); 9 Console.WriteLine($"ToString: {stringValue}"); // Output: 123 10 } 11}
13. ToType
csharp1using System; 2 3class Program 4{ 5 static void Main() 6 { 7 object obj = "123"; 8 int intValue = (int)Convert.ChangeType(obj, typeof(int)); 9 Console.WriteLine($"ToType (ChangeType): {intValue}"); // Output: 123 10 } 11}
14. ToUInt16
csharp1using System; 2 3class Program 4{ 5 static void Main() 6 { 7 object obj = 65535; // Maximum value for UInt16 8 ushort uint16Value = Convert.ToUInt16(obj); 9 Console.WriteLine($"ToUInt16: {uint16Value}"); // Output: 65535 10 } 11}
15. ToUInt32
csharp1using System; 2 3class Program 4{ 5 static void Main() 6 { 7 object obj = 4294967295; // Maximum value for UInt32 8 uint uint32Value = Convert.ToUInt32(obj); 9 Console.WriteLine($"ToUInt32: {uint32Value}"); // Output: 4294967295 10 } 11}
16. ToUInt64
csharp1using System; 2 3class Program 4{ 5 static void Main() 6 { 7 object obj = 18446744073709551615; // Maximum value for UInt64 8 ulong uint64Value = Convert.ToUInt64(obj); 9 Console.WriteLine($"ToUInt64: {uint64Value}"); // Output: 18446744073709551615 10 } 11}