static void Main(string[] args) { var fido = new Dog() { Id = 1, Name = "Fido", Breed = "Pug" }; fido.GoTo("Park"); fido.MakeNoise(); //IAnmial is parent type of Dog. //Dog is subtype of IAnimal var animals = new IAnimal[2]; animals[0] = fido; animals[1] = new Eagle() { Id = 2, Name = "Chirpy" }; foreach (var Item in animals) { Console.WriteLine(Item.Name); Item.MakeNoise(); } }
static void Main(string[] args) { Console.WriteLine("Hello World!"); Dog fido1 = new Dog(); fido1.AnimalId = 1; fido1.Name = "Fido"; fido1.Breed = "Doberman"; Dog fido2 = new Dog { AnimalId = 1, Name = "Fido", Breed = "Doberman", }; fido1.GoTo("Park"); fido1.MakeNoise(); IAnimal animal = fido1; //animal.Breed = ""; //Bird bird = (Bird)animal; Dog dog3 = (Dog)animal; int integer = (int)3.4; double num = integer; var animals = new IAnimal[2]; animals[0] = fido1; animals[1] = new Eagle { AnimalId = 3, Name = "Bill" }; foreach (var item in animals) { Console.WriteLine(item.Name); item.MakeNoise(); item.GoTo("Park"); } MakeNoise(dog3);//upcasted to the function below with the static call. }
static void Main(string[] args) { Console.WriteLine("Hello World!"); var fido1 = new Dog(); fido1.AnimalId = 1; fido1.Name = "Fido"; fido1.Breed = "Doberman"; var fido2 = new Dog { AnimalId = 1, Name = "Fido", Breed = "Doberman" }; fido1.GoTo("park"); fido1.MakeNoise(); IAnimal animal = fido1; //animal.Breed = ""; //Bird bird = (Bird)animal; Dog dog3 = (Dog)animal; int integer = (int)3.4; double num = integer; var animals = new IAnimal[2]; animals[0] = fido1; animals[1] = new Eagle { AnimalId = 3, Name = "Bill" }; foreach (var item in animals) { Console.WriteLine(item.Name); item.MakeNoise(); item.GoTo("park"); } Eagle eagle1 = (Eagle)animals[1]; //eagle.GoTo MakeNoise(dog3); // use camelcase for local variables and private fields // TitleCase aka PascalCase for everything else }
static void Main(string[] args) { Console.WriteLine("Hello World!"); var fido1 = new Dog(); fido1.ID = 1; fido1.Name = "Fido"; fido1.Breed = "Doberman"; var fido2 = new Dog() { ID = 2, Name = "Fido", Breed = "DoberMan" }; fido1.GoTo("park"); fido1.MakeNoise(); IAnimal animal = fido1; Dog dsdf = (Dog)animal; //Bird twdsf = (Bird)animal; int integer = (int)3.5; Console.WriteLine(integer); Console.WriteLine("**********************"); var animals = new IAnimal[2]; animals[0] = fido1; animals[1] = new Eagle() { ID = 1, Breed = "ere2e" }; foreach (var item in animals) { Console.WriteLine(item.Breed); item.MakeNoise(); item.GoTo("park"); } Console.ReadLine(); }
static void Main(string[] args) { var scotch = new Dog(); scotch.Id = 1; scotch.Name = "Scotch"; scotch.Breed = "Labrodoodle"; var badge = new Dog() { Id = 2, Name = "badge", Breed = "Airedale" }; scotch.GoTo("Window"); scotch.MakeNoise(); IAnimals animal = scotch; var animals = new IAnimals[4]; animals[0] = scotch; animals[1] = badge; animals[2] = new Bird { Name = "Olive", Id = 3 }; animals[3] = new Dog { Name = "Sam", Id = 4 }; foreach (IAnimals item in animals) { Console.WriteLine(item.Name); item.MakeNoise(); } //MakeNoise(animals[3]); }
static void Main(string[] args) { Console.WriteLine("Hello World!"); var fido1 = new Dog(); fido1.AnimalId = 0; fido1.name = "fido"; fido1.Breed = "Doberman"; var fido2 = new Dog { AnimalId = 1, name = "fido", Breed = "Doberman" }; fido1.GoTo("park"); fido1.MakeNoise(); //upcasting is automatic //downcasting must be explicit, since it is not guaranteed to succeed int integer = (int)3.4; var animals = new IAnimal[2]; animals[0] = fido1; animals[1] = new Eagle { AnimalId = 3, name = "Bill" }; //hiding only works when variable is the type of the lower! //So it shouldn't be used foreach (IAnimal item in animals) { Console.WriteLine(item.name); item.MakeNoise(); } }
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 }
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 }
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 }