Example #1
0
        static void Main(string[] args)
        {
            //Create a new Animal object
            Animal spot = new Animal(15, 10, "Spot", "Woof");

            //Print info about the Animal object
            Console.WriteLine("{0} says {1}", spot.name, spot.sound);

            //Get the number of Animal objects. You need the class name for static methods
            //as they belong to the class itself, not the objects
            Console.WriteLine("Number of Animals = " + Animal.getNumOfAnimals());

            //Print the Animal object's information
            Console.WriteLine(spot.toString());



            //METHOD OVERLOADING:
            //-------------------------
            Console.WriteLine(spot.getSum(1, 2));                 //The integer version
            Console.WriteLine(spot.getSum(1.4, 2.7));             //The double version
            Console.WriteLine(spot.getSum(num2: 2.7, num1: 1.4)); //The different-order version

            //Create a new Animal object using the Object Initialiser:
            Animal grover = new Animal
            {
                name   = "Grover",
                height = 16,
                weight = 18,
                sound  = "Grrrr"
            };

            //The total number of Animals has increased
            Console.WriteLine("Number of Animals = " + Animal.getNumOfAnimals());

            Dog spike = new Dog();

            Console.WriteLine(spike.toString());//Display the default initialised Dog Object spike

            //Overwrite spike's information
            spike = new Dog(20, 15, "Spike", "Grr", "Chicken");

            Console.WriteLine(spike.toString());//Display the overwritten Dog Object spike



            //POLYMORPHISM USING ABSTRACT CLASSES:
            //----------------------------------------------------------------------------------------------------------
            //Both Shapes but one is a Rectangle and the other a Triangle
            //Since both are Shapes, they can go into Shape arrays despite technically being different classes
            Shape rect = new Rectangle(5, 5);
            Shape tri  = new Triangle(5, 5);

            //Polymorphism makes it so that the correct area() method is called for each
            Console.WriteLine("Area of rect = " + (rect.area()));
            Console.WriteLine("Area of tri = " + (tri.area()));

            //The following will work thanks to the Operator Overloading done in the Rectangle class
            Rectangle combRect = new Rectangle(5, 5) + new Rectangle(5, 5);

            //Display combRect's area
            Console.WriteLine("Area of combRect = " + (combRect.area()));



            //GENERICS CONTINUED:
            //-----------------------------------------------------
            KeyValue <string, string> superman = new KeyValue <string, string>("", "");

            superman.key   = "Superman";
            superman.value = "Clark Kent";

            KeyValue <int, string> samsungTV = new KeyValue <int, string>(0, "");

            samsungTV.key   = 12345;
            samsungTV.value = "a 50-inch Samsung TV";

            superman.showData();
            samsungTV.showData();



            //ENUMERATED TYPES(ENUMs) CONTINUED:
            //---------------------------------------------------
            Temperature micTemp = Temperature.Warm;

            switch (micTemp)
            {
            case Temperature.Freeze:
                Console.WriteLine("Temp on Freezing");
                break;

            case Temperature.Low:
                Console.WriteLine("Temp on Low");
                break;

            case Temperature.Warm:
                Console.WriteLine("Temp on Warm");
                break;

            case Temperature.Boil:
                Console.WriteLine("Temperature on Boil");
                break;
            }



            //STRUCTS CONTINUED:
            //------------------------------------------------------------------------
            Customers bob = new Customers();

            bob.createCust("Bob", 15.50, 12345);

            bob.showCust();



            //DELEGATES CONTINUED:
            //------------------------------------------------------------------------
            //An anonymus method has no name and it's return type is defined by the return used in the method
            GetSum sum = delegate(double num1, double num2)
            {
                return(num1 + num2);
            };

            Console.WriteLine("5 + 10 = " + sum(5, 10));

            //Lamda expressions: Used to act as an anonymus function or an expression trait
            //Can assign the Lamda expression to a function instance
            Func <int, int, int> getSum = (x, y) => x + y;

            Console.WriteLine("5 + 3 = " + getSum.Invoke(5, 3));

            //They're often used with lists:
            List <int> numList = new List <int> {
                5, 10, 15, 20, 25
            };

            List <int> oddNums = numList.Where(n => n % 2 == 1).ToList();//Place all odd numbers in the oddNums List

            foreach (int num in oddNums)
            {
                Console.WriteLine(num + ", ");
            }



            //FILE I/O:
            //-----------------------------------------------------------------------------------------------
            string[] custs = new string[] { "Tom", "Paul", "Greg" };

            //Create custs.txt if it doesn't exist and write to it:
            //HAD TO MAKE CustomerStorage FOLDER MYSELF!!!!!!!
            using (StreamWriter sw = new StreamWriter("CustomerStorage/custs.txt"))
            {
                foreach (string cust in custs)
                {
                    Console.WriteLine("Adding {0} to the file", cust);
                    sw.WriteLine(cust);
                }
            }

            //Read the contents of custs.txt until you reach the end:
            string custName = "";

            //HAD TO MAKE CustomerStorage FOLDER MYSELF!!!!!!!
            using (StreamReader sr = new StreamReader("CustomerStorage/custs.txt"))
            {
                while ((custName = sr.ReadLine()) != null)
                {
                    Console.WriteLine(custName);//Display the contents of custs.txt
                }
            }



            //Used to keep the window open. C#'s version of C's getchar()
            Console.ReadKey();
        }