What is type casting, what's the use of it? How does it work?
5 Answers
Casting is usually a matter of telling the compiler that although it only knows that a value is of some general type, you know it's actually of a more specific type. For example:
object x = "hello";
...
// I know that x really refers to a string
string y = (string) x;
There are various conversion operators. The (typename) expression
form can do three different things:
- An unboxing conversion (e.g. from a boxed integer to
int
) - A user-defined conversion (e.g. casting
XAttribute
tostring
) - A reference conversion within a type hierarchy (e.g. casting
object
tostring
)
All of these may fail at execution time, in which case an exception will be thrown.
The as
operator, on the other hand, never throws an exception - instead, the result of the conversion is null
if it fails:
object x = new object();
string y = x as string; // Now y is null because x isn't a string
It can be used for unboxing to a nullable value type:
object x = 10; // Boxed int
float? y = x as float?; // Now y has a null value because x isn't a boxed float
There are also implicit conversions, e.g. from int
to long
:
int x = 10;
long y = x; // Implicit conversion
Does that cover everything you were interested in?

- 1,421,763
- 867
- 9,128
- 9,194
-
3@jon Skeet gives answer and it doesn't cover everything it has happened ever ? – Shrivallabh Jun 17 '15 at 10:11
-
Read [this](http://stackoverflow.com/questions/2690866/what-is-the-purpose-of-a-question-mark-after-a-type-for-example-int-myvariabl) to see what the question mark does after the `float` type. – martijnn2008 Oct 18 '16 at 08:29
Casting means creating a reference to an object that is of a different type to the reference you're currently holding. You can do upcasting or downcasting and each has different benefits.
Upcasting:
string greeting = "Hi Bob";
object o = greeting;
This creates a more general reference (object) from the more specific reference (string). Maybe you've written code that can handle any object, like this:
Console.WriteLine("Type of o is " + o.GetType());
That code doesn't need to be changed no matter what objects you set o to.
Downcasting:
object o = "Hi Bob";
string greeting = (string)o;
Here you want a more specific reference. You might know that the object is a string (you can test this e.g.:
if (o is string)
{ do something }
Now you can treat the reference as a string instead of an object. E.g. a string has a length (but an object doesn't), so you can say:
Console.WriteLine("Length of string is " + greeting.length);
Which you can't do with an object.

- 2,584
- 1
- 23
- 30
Because C# is statically-typed at compile time, after a variable is declared, it cannot be declared again or used to store values of another type unless that type is convertible to the variable's type
...
However, you might sometimes need to copy a value into a variable or method parameter of another type. For example, you might have an integer variable that you need to pass to a method whose parameter is typed as double. Or you might need to assign a class variable to a variable of an interface type. These kinds of operations are called type conversions. In C#, you can perform the following kinds of conversions

- 1
- 1

- 113,561
- 39
- 200
- 288
-
From the link: "However, if a conversion cannot be made without a risk of losing information, the compiler requires that you perform an explicit conversion, which is called a cast." Is it saying casting is the same as explicit conversion? – Kyle Delaney Feb 13 '17 at 00:41
Also, if you're explicitly casting, you can take advantage of pattern matching. If you have an object:
object aObject = "My string value";
You can safely cast the object as a string in a single line:
if (aObject is string aString)
{
Console.WriteLine("aString = " + aString)
// Output: "aString = My string value"
}
Using this, along with an inverted if statement, you can safely cast types, and fail out early if need be:
public void Conversion(object objA, object objB)
{
// Fail out early if the objects provided are not the correct type, or are null
if (!(objA is string str) || !(objB is int num)) { return; }
// Now, you have `str` and `num` that are safely cast, non-null variables
// all while maintaining the same scope as your Conversion method
Console.WriteLine("str.Length is " + str.Length);
Console.WriteLine("num is " + num);
}

- 2,989
- 5
- 25
- 48