Writing the Main function code
That's that for our class definitions. Let's move on and get into the body of Main
to create our main program. Type the following within the Main
method:
Shape shape = new Rectangle(4, 5);
On the left-hand side, we have Shape
because both Circle
and Rectangle
derive from Shape
. We don't have any code in the Shape
class to define shapes. We are using it simply so that we can write Shape
on the left-hand side to create new objects from that parent class. We're creating a rectangle, so we need to define the dimensions as 4
by 5
.
Next, we come to the subject of this chapter: our switch
block. On the next line, type the following:
switch(shape) { }
This is a new feature of C# 7.0. It is possible to switch between different data types, such as Shape
, for example. Inside the curly braces of the switch
block, type the following:
case Rectangle r when (r.Width == r.Height):
We're doing some really simple math here to check whether a rectangle is actually a square. We...