Beispiel #1
0
        static void Main(string[] args)
        {
            Console.WriteLine("***** The Employee Class Hierarchy *****\n");

            // Give each employee a bonus?
            Employee chucky = new Manager("Chucky", 50, 100000, 9000);

            chucky.GiveBonus(300);
            chucky.DisplayStats();
            Console.WriteLine();

            Employee fran = new SalesPerson("Fran", 43, 3000, 31);

            fran.GiveBonus(200);
            fran.DisplayStats(); // Watch the effect of method's new keyword here
            Console.ReadLine();

            // Casting
            Manager toManager = (Manager)chucky; // Explicit cast
            // as keyword
            Manager isItAManager = chucky as Manager;

            if (isItAManager == null)
            {
                Console.WriteLine("Sorry he is not a manager!");
            }
            // is keyword
            if (chucky is Manager)
            {
                Console.WriteLine("He is manager.");
            }
            // C# 7.0 introduces the notion of patterns, which,
            // abstractly speaking, are syntactic elements that can test that a value has
            // a certain "shape", and extract information from the value when it does
            PrintStars(3);
            object o = "10";

            if (o is int i || (o is string s && int.TryParse(s, out i)))
            {
                Console.WriteLine("i is: {0}", i);
                /* use i */
            }
        }
Beispiel #2
0
        static void Main(string[] args)
        {
            Console.WriteLine("***** The Employee Class Hierarchy *****\n");

            // Give each employee a bonus?
            Employee chucky = new Manager("Chucky", 50, 100000, 9000);
            chucky.GiveBonus(300);
            chucky.DisplayStats();
            Console.WriteLine();

            Employee fran = new SalesPerson("Fran", 43, 3000, 31);
            fran.GiveBonus(200);
            fran.DisplayStats(); // Watch the effect of method's new keyword here
            Console.ReadLine();

            // Casting
            Manager toManager = (Manager)chucky; // Explicit cast
            // as keyword
            Manager isItAManager = chucky as Manager;
            if (isItAManager == null)
            {
                Console.WriteLine("Sorry he is not a manager!");
            }
            // is keyword
            if (chucky is Manager)
            {
                Console.WriteLine("He is manager.");
            }
        }