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 Cast Operator () in C#?
The cast operator () in C# is used for explicit type conversion, converting one data type to another when an implicit conversion is not available or when you want to force a specific conversion. It requires you to explicitly specify the target type in parentheses before the value or variable.
Syntax
Following is the syntax for using the cast operator −
(target_type) expression
Where target_type is the type you want to convert to, and expression is the value or variable being converted −
int result = (int) doubleValue; float floatResult = (float) decimalValue;
Using Cast Operator for Numeric Conversions
The most common use of cast operator is converting between numeric types, especially when precision loss may occur −
Example
using System;
class Program {
static void Main(string[] args) {
double a = 4563.56;
int x;
x = (int)a;
Console.WriteLine("Original double value: " + a);
Console.WriteLine("Casted to int: " + x);
// Multiple casting examples
float f = 123.45f;
byte b = (byte)f;
Console.WriteLine("Float " + f + " casted to byte: " + b);
}
}
The output of the above code is −
Original double value: 4563.56 Casted to int: 4563 Float 123.45 casted to byte: 123
Using Cast Operator for Reference Types
Cast operator can also be used to convert between reference types in inheritance hierarchies −
Example
using System;
class Animal {
public virtual void MakeSound() {
Console.WriteLine("Animal sound");
}
}
class Dog : Animal {
public override void MakeSound() {
Console.WriteLine("Woof!");
}
public void Bark() {
Console.WriteLine("Dog is barking");
}
}
class Program {
static void Main(string[] args) {
Animal animal = new Dog();
animal.MakeSound();
// Casting Animal reference back to Dog
Dog dog = (Dog)animal;
dog.Bark();
}
}
The output of the above code is −
Woof! Dog is barking
Cast Operator vs Other Conversion Methods
| Method | Use Case | Example |
|---|---|---|
| Cast Operator () | Direct type conversion | (int)doubleValue |
| Convert.ToInt32() | Safe conversion with range checking | Convert.ToInt32(stringValue) |
| as operator | Safe reference type casting | obj as MyClass |
Important Considerations
When using cast operator, be aware that it can cause data loss or throw exceptions −
Example
using System;
class Program {
static void Main(string[] args) {
// Data loss example
double precise = 123.789;
int truncated = (int)precise;
Console.WriteLine("Original: " + precise + ", Truncated: " + truncated);
// Overflow example
int largeInt = 300;
byte smallByte = (byte)largeInt;
Console.WriteLine("Large int: " + largeInt + ", Overflowed byte: " + smallByte);
}
}
The output of the above code is −
Original: 123.789, Truncated: 123 Large int: 300, Overflowed byte: 44
Conclusion
The cast operator () in C# enables explicit type conversion between compatible types. While powerful for numeric conversions and reference type casting, it requires careful use to avoid data loss or runtime exceptions, especially when converting between different numeric ranges or reference types.
