예제 #1
0
        /// <summary>method <c>DefineClasses</c>
        ///
        /// - Define a class
        /// - Declare a new instance of a class
        /// - Access properties of an object using dot notation
        /// - Use enumerated types
        /// </summary>
        static void DefineClasses()
        {
            Console.WriteLine("Defining Classes");

            /*
             * Classes are a reference type and an object created based off the properties of a class must be instanciated using the new keyword. That object's type is now tied to the class.
             */

            PetClass pet1 = new PetClass(); // create a new instance of the class

            // define the properties of the new pet object
            pet1.Name     = "Dakota";
            pet1.Age      = 9;
            pet1.Type     = PetType.Dog; // enumerated type define outside of the class
            pet1.Trainned = true;
            // Access each property of the new pet object using dot notation
            Console.WriteLine($"Congrats on your new {pet1.Type}! {pet1.Name} is {pet1.Age} years old. It is {pet1.Trainned} that they are trainned.");

            PetClass pet2 = new PetClass();

            pet2.Name     = "Eddie";
            pet2.Age      = 4;
            pet2.Type     = PetType.Cat;
            pet2.Trainned = true;
            Console.WriteLine($"Congrats on your new {pet2.Type}! {pet2.Name} is {pet2.Age} years old. It is {pet2.Trainned} that they are trainned.");

            PetClass pet3 = new PetClass();

            pet3.Name     = "Bully";
            pet3.Age      = 1;
            pet3.Type     = PetType.Dog;
            pet3.Trainned = false;
            Console.WriteLine($"Congrats on your new {pet3.Type}! {pet3.Name} is {pet3.Age} years old. It is {pet3.Trainned} that they are trainned.");
        }
예제 #2
0
        /// <summary>method <c>UsingLinq</c>
        ///
        /// - Define a list of objects
        /// - LINQ select statement
        /// - ToList method
        /// - First or Default method
        /// </summary>
        static void UsingLinq()
        {
            Console.WriteLine("Using LINQ Query Operations");
            // Define a list of PetClass objects
            List <PetClass> petList = new List <PetClass>();

            petList.Add(new PetClass {
                Name = "Dory", Age = 3, Type = PetType.Fish, Trainned = true
            });
            petList.Add(new PetClass {
                Name = "Taffy", Age = 8, Type = PetType.Dog, Trainned = false
            });
            petList.Add(new PetClass {
                Name = "Midnight", Age = 7, Type = PetType.Cat, Trainned = false
            });
            petList.Add(new PetClass {
                Name = "Ally", Age = 1, Type = PetType.Cat, Trainned = true
            });

            // return an array of cats from the list of pet objects
            List <PetClass> petResults = (from p in petList where p.Type == PetType.Cat select p).ToList();

            Console.WriteLine($"There are {petResults.Count} cats in the list of pet objects!");

            // return an array of dogs from the list of pet objects
            List <PetClass> petResults2 = petList.Where(pet => pet.Type == PetType.Dog).ToList();

            Console.WriteLine($"There are {petResults2.Count} dogs in the list of pet objects!");

            // return the pet names "Dory" form the list of pet objects
            PetClass petResults3 = petList.Where(pet => pet.Name == "Dory").FirstOrDefault();

            Console.WriteLine($"{petResults3.Name} has been found");
        }