public static void Main()
TestCastingFromObjectToDouble();
public static void TestCastingFromObjectToDouble()
Log("The purpose of this test is to show how an object holding an int value can be cast to a double.");
Log("The original int we're working with is int_a = " + int_a);
double dbl_intCastedToDouble = (double)int_a;
Log("We can easily cast this int to a double: " + dbl_intCastedToDouble);
object obj_intCastedToObject = (object)int_a;
Log("We can easily also cast an int to an object: " + obj_intCastedToObject);
Log("If we can cast an int to a double, can we cast the object to a double?");
Log("No. We can't just do double d = (double)obj. If you try you get an System.InvalidCastException.");
Log("Why? Because the object is not a double. It's actually an int.");
double dbl_objectCastedFirstToIntThenDouble = (double)(int)obj_intCastedToObject;
Log("What we can do though is first cast it to an int, then cast it to a double!");
Log("Like this: double dbl_objectCastedFirstToIntThenDouble = (double)(int)obj_intCastedToObject;");
Log("double dbl_objectCastedFirstToIntThenDouble is now " + dbl_objectCastedFirstToIntThenDouble);
Log("Alternatively instead of casting we could use: Convert.ToDouble(obj_intCastedToObject);");
public static void Log(string msg)