static void Main(string[] args) { Text text = new Text(); // Upcasting - both text and shape refence the same // object in memory. Shape shape = text; // To test the reference text.Width = 200; shape.Width = 100; Console.WriteLine(text.Width); // 100 StreamReader reader = new StreamReader(new MemoryStream()); var list = new ArrayList(); list.Add(1); list.Add("Joe"); list.Add(new Text()); var anotherList = new List<Shape>(); Shape shape1 = new Text(); // Downcasting - so text1 will have access to all derived class members Text text1 = (Text)shape; Console.ReadLine(); }
static void Main(string[] args) { // upcasting Text text = new Text(); Shape shape = text; text.Width = 200; shape.Width = 100; Console.WriteLine(text.Width); //StreamReader reader = new StreamReader(new MemoryStream()); ArrayList list = new ArrayList(); list.Add(1); list.Add("Simon"); list.Add(new Text()); // downcasting Shape shape1 = new Text(); Text text1 = (Text)shape; }
public static void DownCasting() { Shape shape = new Text(); Text text = (Text)shape; }