#1,061 – Explicit Reference Conversions
March 26, 2014 Leave a comment
An explicit reference conversion is a conversion between reference types that requires a cast operator. Explicit reference conversions are not guaranteed to succeed and require a check at run-time to determine whether the conversion is allowed. Because the check is done at run-time, the cast operator allows the conversion at compile-time.
Here are some examples:
delegate void StringDel(string info); static void DelMethod(string info) { } static void Main(string[] args) { object o1 = new Dog("Bowser"); dynamic dyn = o1; object o2 = new Cow("Bessie"); Dog d2 = new BorderCollie("Shep"); // From object or dynamic to ref type Dog d = (Dog)o1; d = dyn; // From base class to more derived type BorderCollie bc1 = (BorderCollie)d2; // From base class to interface that the // class does not implement, but that // the actual object does implement. // E.g. Dog does not implement IRun, // but BorderCollie inherits from // Dog and does implement IBark. // d2 is of type Dog, but contains // a BorderCollie. IRun ir1 = (IRun)d2; // From interface type to class that // implements the interface BorderCollie bc2 = (BorderCollie)ir1; // From one interface to the other, provided // that underlying object implements the // target interface. // E.g. BorderCollie implements both IRun // and IFetch, ir1 is of type IRun but // refers to a BorderCollie. We can // convert from variable of type IRun // to one of type IFetch IFetch if1 = (IFetch)ir1; // From one array to another, provided that // explicit conversion exists between element // types. Dog[] dogs = { new Dog("Shep"), new Dog("Kirby") }; object[] dogsAsObjs = dogs; Dog[] dogs2 = (Dog[])dogsAsObjs; // From System.Array to a specific array type Array arrints = new int[2]; int[] ints = (int[])arrints; // From array to IList<>, if explicit conversion from // array element type to IList type exists. object[] objs = new Dog[2]; IList<Dog> dogList = (IList<Dog>)objs; // From IList<> to array, if explicit conversion from // IList type to array element type exists Dog[] objs2 = new Dog[2]; IList<object> dogList2 = objs2; // implicit Dog[] dogs3 = (Dog[])dogList2; // explicit // From Delegate to a specific delegate type StringDel myDel = DelMethod; Delegate genDel = myDel; // implicit StringDel myDel2 = (StringDel)genDel; // explicit }