static void Main(string[] argv) { //structs //create the object, assign values, print it Rectangle rect1; rect1.lenght = 200; rect1.width = 50; Console.WriteLine("Area of react 1 : {0}", rect1.Area()); Rectangle rect2 = new Rectangle(100, 40); rect2 = rect1; rect1.lenght = 33; Console.WriteLine("Area of rectan 2 : {0}", rect2.lenght); //classes - teh main diference between structs is that //classes has inerithence //this exist only for stopping the console from opening Animal fox = new Animal() { name = "red", sound = "raww" }; Animal cat = new Animal("Hannah", "meow"); fox.MakeSound(); cat.MakeSound(); Console.WriteLine("# of animals : {0}", Animal.GetNumAnimal()); //using shapeMath class Console.WriteLine("area of rectangle {0}", ShapeMath.GetArea("rectangle", 5, 5)); //null int?randNum = null; //need ? to be assigned //check if is null if (randNum == null) { Console.WriteLine("randNum is null"); } Console.ReadLine(); }
static void Main(string[] args) { // Create a Rectangle Rectangle rect1; // Add values to it and run the Area method rect1.length = 200; rect1.width = 50; Console.WriteLine("Area of rect1 : {0}", rect1.Area()); // Use a constructor to create a Rectangle Rectangle rect2 = new Rectangle(100, 40); // If you assign one Rectangle to another // you are setting the values and not // creating a reference rect2 = rect1; rect1.length = 33; Console.WriteLine("rect2.length : {0}", rect2.length); // ----- OBJECT ORIENTED PROGRAMMING ----- // A class models real world objects by // defining their attributes (fields) and // capabilities (methods) // Then unlike with structs you can // inherit from a class and create more // specific subclass types // Add a class Project -> Add Class // Create an Animal object // You could also assign values like // fox.name = "Red" Animal fox = new Animal() { name = "Red", sound = "Raaaw" }; // Call the static method Console.WriteLine("# of Animals {0}", Animal.GetNumAnimals()); // You can also create static utility // classes Project -> Add Class Console.WriteLine("Area of Rectangle : {0}", ShapeMath.GetArea("rectangle", 5, 6)); // ----- NULLABLE TYPES ----- // Data types by default cannot have a // value of null. Often null is needed // when you are working with databases // and you can create a null type by // adding a ? to the definition int?randNum = null; // Check for null if (randNum == null) { Console.WriteLine("randNum is null"); } // Another check for null if (!randNum.HasValue) { Console.WriteLine("randNum is null"); } Console.ReadLine(); }
public static void practice1() { Console.WriteLine(String.Empty); Console.WriteLine("Struct"); Console.WriteLine("======================="); Rectangle rect1; rect1.width = 100; rect1.length = 30; rect1.print(); // Console.WriteLine("[L] width{0}, length:{1}, Area{2}", rect1.width, rect1.length, rect1.Area()); Rectangle rect2 = new Rectangle(200, 50); rect2.print(); //Console.WriteLine("[L] width{0},, length:{1}, Area{2}", rect2.width, rect2.length, rect2.Area()); Console.WriteLine(String.Empty); Console.WriteLine("Class Animal"); Console.WriteLine("======================="); List <Animal> animals = new List <Animal>(); animals.Add(new Animal("fox", "Raww")); animals.Add(new Animal("dog", "Woof")); animals.Add(new Animal("cat", "Mewww")); /* // 바로 아래 foreach와 같음 * for (int i = 0; i < animals.Count; i++) * { * Animal thisAnimal = animals[i]; * } */ bool bFound = false; foreach (var animal in animals) { var animalName = animal.GetName(); if (animalName == "pig") { bFound = true; break; } } if (bFound) { Console.WriteLine("pig found"); } else { Console.WriteLine("pig not found"); } foreach (var animal in animals) { //var animalNamePub = animal.name; - private이기떄문에 안됨 var animalName = animal.GetName(); if (animalName == "cat") { Console.WriteLine("Found cat"); Console.WriteLine(">>"); animal.MakeSound(); } //animal.MakeSound(); } Animal myCat = null; // = Animal myCat = animals.Find( ... myCat = animals.Find(item => item.GetName().Equals("cat")); if (myCat != null) { Console.WriteLine("Found my cat again"); Console.WriteLine(">>"); myCat.MakeSound(); } Animal myPig = animals.Find(item => item.GetName().Equals("pig")); if (myPig != null) { Console.WriteLine("Found pig"); myPig.MakeSound(); } else { Console.WriteLine("pig not found"); } Dictionary <enAnimalType, Animal> dicAnimals = new Dictionary <enAnimalType, Animal>(); dicAnimals.Add(enAnimalType.fox, new Animal("red", "Raww")); dicAnimals.Add(enAnimalType.dog, new Animal("blue", "Woof")); dicAnimals.Add(enAnimalType.cat, new Animal("pink", "Meww")); var someAnimal = dicAnimals[enAnimalType.cat]; // foreach (var animal in animals), Animal my Cat (위의 2개) 2개 하고 동일 foreach (KeyValuePair <enAnimalType, Animal> item in dicAnimals) { var key = item.Key; var value = item.Value; value.MakeSound(); } foreach (var item in dicAnimals.Values) { item.MakeSound(); } Animal outAnimal; if (dicAnimals.TryGetValue(enAnimalType.pig, out outAnimal)) { outAnimal.MakeSound(); } else { Console.WriteLine("[E] pig not found"); } var pig = dicAnimals[enAnimalType.fox]; /* //위랑 같은내용 * var cat = new Animal("고양이", "Mewww"); * * cat.MakeSound(); * * * var dog = new Animal("개", "woof"); * * * dog.MakeSound(); * * * var fox = new Animal("여우", "How"); // Animal = Class, fox = instance, new Animal = 생성자, ("여우","How") = 파라미터 * * fox.MakeSound(); // function */ Console.WriteLine("numberOfAnimals : {0}", Animal.GetNumOfAnimals()); Console.WriteLine(String.Empty); Console.WriteLine("ShapeMath"); Console.WriteLine("=========================="); Console.WriteLine("Area of Rectangle : {0}", ShapeMath.GetArea(enShape.Rectangle, 5, 6)); Console.WriteLine("Area of Rectangle : {0}", ShapeMath.GetArea(enShape.Triangle, 5, 6)); Console.WriteLine("Area of Rectangle : {0}", ShapeMath.GetArea(enShape.Circle, 5)); Console.ReadLine(); }