示例#1
0
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");

            // c# has property initializer syntax
            Dog fido1 = new Dog();

            fido1.Id    = 1;
            fido1.Name  = "Fido";
            fido1.Breed = "Husky";

            // property initializer syntax
            Dog fido2 = new Dog
            {
                Id    = 1,
                Name  = "Fido",
                Breed = "Husky"
            };

            fido1.GoTo("park");
            fido1.MakeNoise();

            // IAnimal is a parent type of Dog
            // Dog is a subtype of IAnimal
            IAnimal animal = fido1;
            // converting from dog var to IAnimal var is upcasting
            // upcasting is guaranteed to succeed, so it's implicit

            // converting the other was is downcasting, not guar to succeed
            // must be explicit with () casting
            //Bird bird = (Bird)animal;
            Dog dog3 = (Dog)animal;

            // not all casting is upcasting or downcasting e.g. int to double and back
            // double to int loses data, "unsafe", so must be explicit
            int integer = (int)3.4;

            //int to double cannot lose data, safe, can do with implicit cast
            double num = integer;

            var animals = new IAnimal[2];

            animals[0] = fido1;
            animals[1] = new Eagle()
            {
                Id   = 3,
                Name = "Woody"
            };

            // animal class can implement as many interfaces as it likes,
            // but a class may only have one direct parent class

            // this code doesnt care how the members are implemented
            // only that they can do the job specified by the interface
            foreach (IAnimal item in animals)
            {
                Console.WriteLine(item.Name);
                item.MakeNoise();
                item.GoTo("park");  // here we can't see Eagle.GoTo which only hides Bird.GoTo
                                    // without truly overriding it
                // once we use virtual/override, it does replace the method implementation
                //on the object itself
            }
            Eagle eagle1 = (Eagle)animals[1];

            eagle1.GoTo("park");

            MakeNoise(dog3); //upcasting here

            // use camelCase for local vars and private fields
            // TitleCase aka PascalCase for everything else
        }
示例#2
0
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");

            Dog fido1 = new Dog();

            fido1.AnimalId = 1;
            fido1.Name     = "Fido";
            fido1.Breed    = "Doberman";

            // C# has "property initializer" syntax.
            Dog fido2 = new Dog
            {
                AnimalId = 1,
                Name     = "Fido",
                Breed    = "Doberman"
            };

            fido1.GoTo("park");
            fido1.MakeNoise();

            // IAnimal is a parent type of Dog
            // Dog is a subtype of IAnimal
            IAnimal animal = fido1;
            // converting from Dog variable to IAnimal variable is "upcasting"
            // upcasting is guaranteed to succeed, so it's implicit

            // Ctrl+K, Ctrl+C to comment lines
            // Ctrl+K, Ctrl+U to uncomment lines
            // when the Dog object is contained in a IAnimal variable,
            // we can't see the Dog-specific stuff anymore.
            //animal.Breed = ""; // error

            // converting the OTHER way, from IAnimal down to Dog, is "downcasting"
            // NOT guaranteed to succeed, so it must be explicit with () casting
            //Bird bird = (Bird)animal;
            Dog dog3 = (Dog)animal;

            // not all casting is upcasting or downcasting, e.g. int to double and back
            // double to int loses dats, "unsafe", so, it must be explicit.
            int integer = (int)3.4;
            // int to double cannot lose data, "safe", so, we can do that with implicit cast.
            double num = integer;

            var animals = new IAnimal[2];

            animals[0] = fido1;
            animals[1] = new Eagle
            {
                AnimalId = 3,
                Name     = "Bill"
            };

            // a class can implement as many interfaces as it likes
            // but, a class may only have one direct parent class

            // this code doesn't care how the members are implemented,
            // only that they can do the job specified by the interface.
            foreach (IAnimal item in animals)
            {
                Console.WriteLine(item.Name);
                item.MakeNoise();
                item.GoTo("park"); // here, when we weren't using vrtiaul/override,
                                   // we can't see Eagle.GoTo, which only hides ABird.GoTo
                                   // without truly overriding it.

                // once we use virtual/override, it really does replace the method implementation
                // on the object itself
            }

            Eagle eagle1 = (Eagle)animals[1];

            eagle1.GoTo("park");

            MakeNoise(dog3); // upcasting here

            // we use camelCase for local variables and private fields
            // TitleCase aka PascalCase for everything else
        }
示例#3
0
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");

            Dog fido1 = new Dog();

            fido1.Id    = 1;
            fido1.Name  = "Fido";
            fido1.Breed = "Doberman";

            //C# has property initializer syntax.
            Dog fido2 = new Dog
            {
                Id    = 1,
                Name  = "Fido",
                Breed = "Doberman"
            };

            fido1.GoTo("park");
            fido1.MakeNoise();

            //IAnimal is a parent type of Dog
            //Dog is a subtype of IAnimal

            IAnimal animal = fido1;
            //Conveting from Dog variable to IAnimal variable is "upcasting"
            //Upcasting is guarenteed to succeed, so it's implicit

            //converting the OTHER way, from IAnimal down to Dog, is "downcasting"
            //NOT guaranteed to succeed, so it must explicit with () casting
            //Bird bird = (Bird)animal; : Error

            //when the Dog object is contained in an IAnimal variable, we can't
            //  see the Dog specific stuff anymore
            //  animal.Breed = ""; //error

            Dog dog3 = (Dog)animal; // : Works

            //not all casting is upcasting or downcasting, e.g. int to double and back
            int    integer = (int)3.4;
            double num     = integer;

            var animals = new IAnimal[2];

            animals[0] = fido1;
            animals[1] = new Eagle()
            {
                Id   = 3,
                Name = "Bill"
            };

            //this code doesn't care how the members are implmented,
            //  only that thye can do the job specified by the interface
            foreach (var item in animals)
            {
                Console.WriteLine(item.Name);
                item.MakeNoise();
                item.GoTo("park"); //here when we weren't using virtual/override
                                   //here we can't see Eagle.GoTo, whinc only hides
                                   //ABird.GoTo without truly overriding it

                //once we use virtual/override, it really does replace the method implementation
                //on the object itself
            }

            Eagle eagle1 = (Eagle)animals[1];

            eagle1.GoTo("park");

            try
            {
                Console.WriteLine("What should the dog say?");
                string input = Console.ReadLine();
                fido1.SetNoise(input);
            }
            catch
            {
                Console.WriteLine("caught ArgumentException! using fallback value");
                fido1.SetNoise("woof");
            }
            Console.WriteLine(fido1.GetNoise());
        }
示例#4
0
        static void Main(string[] args)
        {
            // C# has "property initializer" system
            var fido1 = new Dog();

            fido1.Id    = 1;
            fido1.Name  = "Fido";
            fido1.Breed = "German Shepherd";

            // Much nicer syntax for doing the same thing!!!
            var fido2 = new Dog
            {
                Id    = 1,
                Name  = "Fido",
                Breed = "German Shepherd"
            };

            fido1.GoTo("park");
            fido1.MakeNoise();

            // IAnimal is a parent type of Dog
            // Dog is a subtype of IAnimal
            IAnimal animal = fido1;
            // converting from dog variable to IAnimal variable is upcasting
            // upcasting is guaranteed to exceed so it is implicit

            //when the Dog object is contained in IAnimal Variable,
            // we cannot see the Dog-specific stuff anymore
            // animal.Breed // error

            // Converting from IAnimal to Dog is downcasting and is not guaranteed to succeed
            // must be explicit with () casting

            Dog dog2 = (Dog)animal;

            // not all casting is up/downcasting, e.g. int to double and back
            // double to int loses data and thus must be exclicit
            int integer = (int)3.4;

            // int to double cannot lose data however
            // thus can be done implicitly
            double num = integer;

            var animals = new IAnimal[2];

            animals[0] = fido1;
            animals[1] = new Eagle
            {
                Id   = 3,
                Name = "Bob",
            };

            foreach (IAnimal item in animals)
            {
                Console.WriteLine(item.Name);
                item.MakeNoise();
                item.GoTo("Park"); // here we can't see Eagle.GoTo when using new
                                   // works right when we properly override using virtual and override
            }

            Eagle eagle1 = (Eagle)animals[1];

            eagle1.GoTo("Park");

            // camel case for local variables and private fields
            //Titlecase for everything else
        }