repl.it
@kironroy/

ctrees

C#

No description

fork
loading
Files
  • main.cs
  • DouglasFir.cs
  • main.exe
  • Redwoods.cs
  • SitkaSpruce.cs
  • TreeTypes.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
using System.Collections.Generic;
// https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/polymorphism


namespace Trees
{

    class Program
    {
        static void Main(string[] args)
        {
            // polymorphism at work #1: Redwoods, Douglas Fir and Sitka Spruce
            // can all be used wherever a tree type is expected.  No cast
            // is required because an implicit conversion exists from a derived
            // class to its base class

            var trees = new List<TreeTypes>
            {
               new Redwoods(),
               new DouglasFir(),
               new SitkaSpruce()
            };

            // polymorphism at work #2: the virtual method Planting is 
            // invoked on each of the derived classes, not the base class.

            foreach (var tree in trees)
            {
                tree.Planting();
            }

    }
    }
}