Esempio n. 1
0
        static void Main(string[] args)
        {
            Mammal dog   = new Dog("Jamie");
            Mammal cat   = new Cat("Misha");
            Mammal human = new Human("Stanislava");

            Mammal[] mammals = new Mammal[3];

            mammals[0] = dog;
            mammals[1] = cat;
            mammals[2] = human;

            for (int i = 0; i < mammals.Length; i++)
            {
                Console.WriteLine($"{mammals[i].Name}");
                mammals[i].MakeSomeNoise();
            }

            Console.WriteLine("Press any key to continue...");
            Console.ReadKey();
        }
Esempio n. 2
0
        static void Main(string[] args)
        {
            Enumerable.Range(0, 100)
            .Where(x => Math.Log(x % 10 - 2) is var value && value >= 1 && value <= 2);

            Animal animal = new Animal();
            Mammal mammal = new Mammal();
            Person person = new Person();

            if (person is Person)
            {
                ((Person)person).GetSalary();
            }
            //or
            if (person is Person realPerson)
            {
                realPerson.GetSalary();
            }

            List <Animal> list = new List <Animal>();

            list.Add(person);
            list.Add(mammal);
            list.Add(person);

            foreach (var item in list)
            {
                //different answear for evry class in list (polymorphism)
                item.PrintToConsole();

                if (item is Animal)
                {
                    //Console.WriteLine("I am animal! ");
                    //added as method
                }
                else if (item is Mammal)
                {
                    //Console.WriteLine("I am mammal!");
                }
                else if (item is Person)
                {
                    //Console.WriteLine("I am person!");
                }
            }


            Animal animal3 = new Dog();
            Person person3 = animal as Person;

            person?.GetSalary(); //?. check if its null

            //same as
            if (person != null)
            {
                person.GetSalary();
            }

            //its possible
            int?a = 4;

            a = null;

            //operator ??
            Person person4 = animal as Person ?? new Person();
            //if left side doesnt match do right side
        }