Casting Options
Before C# 4.0, C# was mainly a strongly typed language. Even with the new dynamic features introduced in version 4.0, it is still considered mainly a strongly type language. With strongly typed languages the compiler typically is able to warn us about possible type mismatch issues but this is not true 100% of the time. There also times where we have to change the type of a value we are working with. This is very common when working with arguments passed by most event handlers. Consider the following function:
private void ListBoxOne_MouseLeftButtonUp(object sender, MouseButtonEventArgs e) { // Capture selected item data ListBox lb = (ListBox) sender; _selectedItem = lb.SelectedIndex; // Start page transition animation PageTransitionList.Begin(); }
I needed to get the value of the selected item on a ListBox. A reference to the listbox was passed to the event handler with the type of object which is the most basic type in .Net. Every type is derived from the object class. Now since I know that the sender parameter is of type ListBox (being passed as an object), I can simply to a cast and grab the property that I am interested in. But there is an problem with this. What sender is not of type ListBox? This is cause my code to error out because the cast would be invalid. See the compiler can’t really determine ahead of time the true type of sender in order to warn us. And seems we are explicitly casting it to ListBox the compiler assumes we know that we are doing ;-). The proper was to do this cast would be to use the as operator and the check for a null value. The as operator returns a null if the cast can’t be perform.
// Capture selected item data ListBox lb = sender as ListBox; if (lb != null) _selectedItem = lb.SelectedIndex; // Start page transition animation PageTransitionList.Begin();
Some time we can cast without having to explicitly define the type we want to cast to. For example, we can cast an int to a float without explicitly telling the compiler our intentions. That is because the type float has more precision than int so no data can be lost. The following code works.
int i = 1; float f = i;
However, if you assign f back to i, the compiler will error out. You must explicitly tell the compiler of your intents. This is because there is a possibility of data lost going from a float to and int since float has more precision than an int.
int i = 1; float f = i; f= i;
Same is true for reference types when casting to a from derive types. You have to explicitly cast is going from a parent type to a child type. Implicit cast is allowed from child to parent.
public class Car { public string Make; public string Model; } public class RasingCar : Car { public bool SteetLegal; } public class SomeClass { public SomeClass() { Car c = new Car(); RasingCar rc = new RasingCar(); //allowed c = rc; //Not allowed rc = c; } }
However if you are sure about the cast, you can always explicitly make the cast. The following code will work. Just remember to check for null ;-)
rc = c as RasingCar;
Now, there are times where you want to control how reference type get “casted” from one type to the other. Lets say you have a Meters and Foot/Feet type to keep track of measurements. At times you might want to convert from meters to feet or the other way around. You can sure do this with a simple helpers function or you can make your classes aware of the conversion. Using the explicit operator keywords we can accomplish this.
class Foot { private float _length; public float Length { get { return _length; } } public Foot(float l) { _length = l; } public static explicit operator Meters(Foot f) { return new Meters((12 / 39.37f) * f.Length); } public override string ToString() { return Length.ToString(); } } class Meters { private float _length; public float Length { get { return _length; } set { _length = value; } } public Meters(float length) { _length = length; } public static explicit operator Foot(Meters m) { return new Foot(3.28084f * m._length); } public override string ToString() { return Length.ToString(); } }
So now that both classes are aware of one another, we can easily make a cast and expect the right value to be assign.
static void Main() { Foot f = new Foot(100); Meters m = new Meters(100); Console.WriteLine("{0} meters is {1} feet", m.Length, (Foot)m); Console.WriteLine("{0} feet is {1} meters", f.Length, (Meters)f); Console.Read(); }
Hope this helps.
3 comment(s) so far
nice, but this is nothing new in.. this type of casting has been available long before 4.0
My intent was to provide a simple explanation for folks getting started. I will be adding some 4.0 content soon

nice, but this is nothing new in.. this type of casting has been available long before 4.0