Type casting means converting a variable of one data type into another. In C#, it is required when you want to assign a value of one type to a variable of another type.
For example:
C#
int num = 10;
double d = num; // int is converted to double (type casting)
Type Casting can be divided into two parts:
- Implicit Type Conversion (Type Safe)
- Explicit Type Conversion (Manual Conversion)
1. Implicit Type Casting
Implicit type casting (type-safe casting) is the process in which the C# compiler automatically converts a smaller (compatible) data type into a larger data type (e.g., int
to double
) without any data loss. It is performed automatically by the compiler.
Implicit Type casting conditionsIn C#, numeric types support implicit conversions among themselves, but not to char or bool. Also, char and bool are incompatible. The compiler ensures type compatibility before conversion.
Example 1:
C#
using System;
namespace Casting {
class Geeks {
public static void Main(String[] args)
{
int i = 57;
// automatic type conversion
long l = i;
// automatic type conversion
float f = l;
Console.WriteLine("Int value :" + i);
Console.WriteLine("Long value :" + l);
Console.WriteLine("Float value :" + f);
}
}
}
OutputInt value 57
Long value 57
Float value 57
Example 2: This program gives a compilation error because we try to convert a large data type into a small type
C#
// Incompatible Data Conversion
using System;
namespace Casting {
class Geeks {
// Main Method
public static void Main(String[] args)
{
double d = 765.12;
// Incompatible Data Type
int i = d;
// Display Result
Console.WriteLine("Value of i is :", +i);
}
}
}
Output:
main.cs(13,17): error CS0266: Cannot implicitly convert type `double' to `int'. An explicit conversion exists (are you missing a cast?)
Compilation failed: 1 error(s), 0 warnings
2. Explicit Type Casting
Explicit type casting is the process of manually converting one data type into another using a cast operator (type). It is required when converting from a larger data type to a smaller one, or between incompatible types and may cause data loss or exceptions.
Explicit Type CastingExample 1: Using Explicit Conversion to convert double into int.
C#
using System;
namespace Casting {
class Geeks {
public static void Main(String[] args){
double d = 765.12;
// Explicit Type Casting larger data into smaller data type
int i = (int)d;
// Display Result
Console.WriteLine("Value of i is " + i);
}
}
}
This solves our some of the problems, but we can also have some available methods that can help us for conversion mentioned below.
Type Conversion Methods
There are some built in methods defined for type conversions mentioned below:
Method | Description |
---|
ToBoolean | It will converts a type to Boolean value |
ToChar | It will converts a type to a character value |
ToByte | It will converts a value to Byte Value |
ToDecimal | It will converts a value to Decimal point value |
ToDouble | It will converts a type to double data type |
ToInt16 | It will converts a type to 16-bit integer |
ToInt32 | It will converts a type to 32 bit integer |
ToInt64 | It will converts a type to 64 bit integer |
ToString | It will converts a given type to string |
ToUInt16 | It will converts a type to unsigned 16 bit integer |
ToUInt32 | It will converts a type to unsigned 32 bit integer |
ToUInt64 | It will converts a type to unsigned 64 bit integer |
Example: Typecasting using built-in methods
C#
using System;
namespace Casting {
class Geeks {
public static void Main(String[] args)
{
int i = 12;
double d = 765.12;
float f = 56.123F;
// Using Built- In Type Conversion Methods & Displaying Result
Console.WriteLine(Convert.ToString(f));
Console.WriteLine(Convert.ToInt32(d));
Console.WriteLine(Convert.ToUInt32(f));
Console.WriteLine(Convert.ToDouble(i));
Console.WriteLine("GeeksforGeeks");
}
}
}
Output56.123
765
56
12
GeeksforGeeks
Explore
Introduction
Fundamentals
Control Statements
OOP Concepts
Methods
Arrays
ArrayList
String
Tuple
Indexers