Exemplo n.º 1
0
        static void Main(string[] args)
        {
            Dog dog = new Dog();

            dog.Name = "Dum";
            dog.Speak();

            Console.ReadLine();
        }
        /* as
         * You can use the as operator to perform certain types of conversions between compatible reference types or nullable types.
         * The as operator is like a cast operation. However, if the conversion isn't possible, as returns null instead of raising an exception.
         */

        public static void ConvertObjectExample()
        {
            Dog dog = new Dog();

            Console.WriteLine("Dog::{0}", dog.GetType());
            dog.Speak();

            // convert dog to animal
            Animal animal;

            animal = dog as Animal;  // still pointing the Dog class

            Console.WriteLine("Animal::{0}", animal.GetType());
            animal.Speak();
        }