Esempio n. 1
0
        public void InterfacesInCollection()
        {
            Orange orange = new Orange();

            // Var allows different fruits using the IFruit interface to exist together
            var fruitSalad = new List <IFruit>
            {
                new Banana(),
                new Orange(),
                orange
            };

            // Orange exclusive methods still accessible outside of IFruit collection
            orange.Squeeze();

            foreach (var fruit in fruitSalad)
            {
                Console.WriteLine(fruit.Name);
                Console.WriteLine(fruit.Peel());
                //fruit.squeeze
                // 'Squeeze' No longer accessible once in a collection

                Assert.IsInstanceOfType(fruit, typeof(IFruit));
            }

            Assert.IsInstanceOfType(orange, typeof(Orange));
        }
Esempio n. 2
0
        public void CallingInterfaceMethods()
        {
            // New up an instance of an Ifruit
            IFruit fruit = new Orange();    //calling just Ifruit props , not banana or orange
                                            // we can change new Banana to Orange or grape and get new output for everything
            string output = fruit.Peel();

            Console.WriteLine(output);

            Console.WriteLine($"The {fruit.Name} is peeled: " + fruit.Peeled);

            var fruits = new Orange();  // but from here I can call orange sqeeze method, because "var" not Ifruit.

            fruits.Squeeze();
        }
Esempio n. 3
0
        public void InterfacesInCollections()
        {
            var orange     = new Orange(); //could put Orange in place of var but did that so this is dry code
            var fruitSalad = new List <IFruit>();

            fruitSalad.Add(orange);
            fruitSalad.Add(new Banana());
            fruitSalad.Add(new Grape());

            foreach (var fruit in fruitSalad)
            {
                Console.WriteLine(fruit.Name);
                Console.WriteLine(fruit.Peel());
            }
            Console.WriteLine(orange.Squeeze()); //can't go inside of foreach loop because fruit in the foreach loop is of type IFruit
        }
Esempio n. 4
0
        public void InterfacesInCollections()
        {
            var orange = new Orange();

            var fruitSalad = new List <IFruit>();

            fruitSalad.Add(orange);
            fruitSalad.Add(new Banana());
            fruitSalad.Add(new Grape());

            foreach (var fruit in fruitSalad)
            {
                Console.WriteLine(fruit.Name);
                Console.WriteLine(fruit.Peel());
            }
            Console.WriteLine(orange.Squeeze());  //this can be called directly because it is only in orange class.  can't be part of the foreach loop
        }
        public void InterfacesInCollections()
        {
            var orange = new Orange();

            var fruitSalad = new List <IFruit>();

            fruitSalad.Add(orange);
            fruitSalad.Add(new Banana());
            fruitSalad.Add(new Grape());

            foreach (var fruit in fruitSalad)
            {
                Console.WriteLine(fruit.Name);
                Console.WriteLine(fruit.Peel());
            }
            Console.WriteLine(orange.Squeeze());
        }