repl.it
@kironroy/

updowncasting

C#

No description

fork
loading
Files
  • main.cs
  • main.exe
  • notes.txt
  • Shape.cs
  • Text.cs
main.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Casting
{

    class Program
    {
        static void Main(string[] args)
        {

            Console.WriteLine("Upcasting below: ");

            Text text = new Text();  // didn't use var to see the Text class
            Shape shape = text; // didn't use var to see the Shape class

            text.Width = 200;
            shape.Width = 100; // Text class inherits from Shape so it will cw 100

            Console.WriteLine($"The value of text width will be: {text.Width}");

            Console.WriteLine(); // breakline

            Console.WriteLine("Downcasting below: ");

            Shape shapeTwo = new Text();
            Text textTwo = (Text) shapeTwo; // downcasting

            textTwo.Width = 300;
            shapeTwo.Width = 150; // this will print below

            Console.WriteLine($"The value of text width will be: {shapeTwo.Width}");










        }
    }
}