コード例 #1
0
        public void InterfacesInMethods()
        {
            var grape  = new Grape();
            var output = GetFruitName(grape);

            Console.WriteLine(output);
            Assert.IsTrue(output.Contains("This fruit is called Grape"));
        }
コード例 #2
0
        public void TypeOfInstance()
        {
            var tomato     = new Grape();
            var fruitSalad = new List <IFruit>
            {
                new Orange(),
                new Grape(),
                new Banana(true),
                new Orange(),
                new Banana(),
                tomato,
            };

            Console.WriteLine("Is the banana peeled?");
            foreach (var fruit in fruitSalad)
            {
                if (fruit is Banana)
                {
                    var banana = (Banana)fruit;
                    if (banana.Peeled)
                    {
                        Console.WriteLine("Yes the banana is peeled!");
                    }
                    else
                    {
                        Console.WriteLine("Sorry bud, this banana isn't peeled.");
                    }
                }
                else if (fruit.GetType() == typeof(Grape))
                {
                    Console.WriteLine("Congrats on the Grape!");
                }
                else
                {
                    Console.WriteLine("Ya got an orange, sorry it's not in juice form :(");
                }
            }
        }