Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
What is the difference between implicit and explicit type conversion in C#?
Type conversion in C# refers to converting a value from one data type to another. There are two main types of conversions: implicit (automatic) and explicit (manual). Understanding when each occurs helps prevent data loss and compilation errors.
Implicit Type Conversion
Implicit conversions are performed automatically by the C# compiler when converting from a smaller data type to a larger one. This is safe because no data is lost in the process.
Syntax
smallerType variable = value; largerType newVariable = variable; // Automatic conversion
Example
using System;
class Program {
static void Main(string[] args) {
int val1 = 34567;
int val2 = 56743;
long sum;
sum = val1 + val2; // Implicit conversion from int to long
Console.WriteLine("Sum = " + sum);
Console.WriteLine("Type of sum: " + sum.GetType());
// More implicit conversion examples
int intValue = 100;
float floatValue = intValue; // int to float
double doubleValue = floatValue; // float to double
Console.WriteLine("Int: " + intValue);
Console.WriteLine("Float: " + floatValue);
Console.WriteLine("Double: " + doubleValue);
}
}
The output of the above code is −
Sum = 91310 Type of sum: System.Int64 Int: 100 Float: 100 Double: 100
Explicit Type Conversion
Explicit conversions must be performed manually using casting or conversion methods. This is required when converting from a larger data type to a smaller one, as data loss may occur.
Syntax
targetType variable = (targetType)sourceVariable; // Cast operator targetType variable = Convert.ToTargetType(sourceVariable); // Convert class
Using Cast Operator
using System;
class Program {
static void Main(string[] args) {
double d = 1234.89;
int i;
// Explicit cast from double to int (decimal part is lost)
i = (int)d;
Console.WriteLine("Original double: " + d);
Console.WriteLine("Converted int: " + i);
// More explicit conversion examples
float f = 123.456f;
int intFromFloat = (int)f;
Console.WriteLine("Float: " + f);
Console.WriteLine("Int from float: " + intFromFloat);
}
}
The output of the above code is −
Original double: 1234.89 Converted int: 1234 Float: 123.456 Int from float: 123
Using Convert Class
using System;
class Program {
static void Main(string[] args) {
string stringValue = "123";
int intValue = Convert.ToInt32(stringValue);
double doubleValue = Convert.ToDouble(stringValue);
Console.WriteLine("String: " + stringValue);
Console.WriteLine("Converted to int: " + intValue);
Console.WriteLine("Converted to double: " + doubleValue);
// Converting boolean
bool boolValue = true;
int boolToInt = Convert.ToInt32(boolValue);
Console.WriteLine("Boolean: " + boolValue);
Console.WriteLine("Boolean to int: " + boolToInt);
}
}
The output of the above code is −
String: 123 Converted to int: 123 Converted to double: 123 Boolean: True Boolean to int: 1
Comparison
| Implicit Conversion | Explicit Conversion |
|---|---|
| Performed automatically by compiler | Must be done manually by programmer |
| Safe conversions only (no data loss) | May result in data loss |
| Smaller to larger data types | Any data type to any compatible type |
| No special syntax required | Requires cast operator or Convert methods |
| Example: int to long | Example: double to int |
Conclusion
Implicit conversions happen automatically for safe type conversions, while explicit conversions require manual intervention and may cause data loss. Choose implicit conversions when widening data types and explicit conversions when narrowing or converting between incompatible types.
