コード例 #1
0
        static void Main(string[] args)
        {
            string firstName;
            string lastName;
            string address;
            string city;
            string province;
            string postalCode;

            Console.Title = "COMP2614 - Assignment 2 - A00838629";

            System.Console.Write("Whats your first name?: ");
            firstName = System.Console.ReadLine();

            System.Console.Write("Whats your last name?: ");
            lastName = System.Console.ReadLine();


            System.Console.Write("Whats your address?: ");
            address = System.Console.ReadLine();

            System.Console.Write("Whats your city?: ");
            city = System.Console.ReadLine();

            System.Console.Write("Whats your province?: ");
            province = System.Console.ReadLine();

            System.Console.Write("Whats your postal code?: ");
            postalCode = System.Console.ReadLine();

            //Calling Parameterized constructor
            Contact[] contact =
            {
                new Contact(firstName, lastName, address, city, province, postalCode)
            };

            ConsolePrinter.PrintMembers(contact);
            //Using Object Initialization
            Contact contact_1 =
                new Contact()
            {
                FirstName = firstName, LastName = lastName, Address = address, City = city, Province = province, PostalCode = postalCode
            };

            //Populate using properties
            Contact contact_2 = new Contact();

            contact_2.FirstName  = firstName;
            contact_2.LastName   = lastName;
            contact_2.Address    = address;
            contact_2.City       = city;
            contact_2.Province   = province;
            contact_2.PostalCode = postalCode;
        }
        /// <summary>
        /// Populates the contact list
        /// </summary>
        private void populateContacts()
        {
            string enterFirstName;
            string enterLastName;
            string enterAddress;
            string enterCity;
            string enterProvince;
            string enterPostalCode;

            // Request for contact information
            requestContactInfo(out enterFirstName, out enterLastName, out enterAddress, out enterCity, out enterProvince, out enterPostalCode);

            // Creates object through property methods
            Contact contactOne = objectOneInit(enterFirstName, enterLastName, enterAddress, enterCity, enterProvince, enterPostalCode);

            // Creates object through parameterized constructor
            Contact contactTwo = new Contact(enterFirstName, enterLastName, enterAddress, enterCity, enterProvince, enterPostalCode);

            // Creates object through Object Initializer syntax
            Contact contactThree = new Contact {
                FirstName = enterFirstName, LastName = enterLastName, Address = enterAddress, City = enterCity, Province = enterProvince, PostalCode = enterPostalCode
            };

            // Creates an array of contacts
            Contact[] contacts = new Contact[3];
            contacts[0] = contactOne;
            contacts[1] = contactTwo;
            contacts[2] = contactThree;

            Console.WriteLine("Contacts");
            Console.WriteLine(new string('-', 30));

            // Loops through the array and prints objects
            foreach (Contact contact in contacts)
            {
                ConsolePrinter.Print(contact);
            }
        }