Example #1
0
        static void Main(string[] args)
        {
            Car car1 = new Car();
            car1.Make = "Oldsmobile";
            car1.Model = "Cutlas Supreme";

            Car car2 = new Car();
            car2.Make = "Geo";
            car2.Model = "Prism";

            Book b1 = new Book();
            b1.Author = "Robert Tabor";
            b1.Title = "Microsoft .NET XML Web Services";
            b1.ISBN = "0-000-00000-0";

            Console.ReadLine();
        }
Example #2
0
        static void Main(string[] args)
        {
            Car car1 = new Car();
            car1.Make = "Oldsmobile";
            car1.Model = "Cutlas Supreme";

            Car car2 = new Car();
            car2.Make = "Geo";
            car2.Model = "Prism";

            Book book1 = new Book();
            book1.Author = "Robert Tabor";
            book1.Title = "Microsoft .NET XML Web Services";
            book1.ISBN = "0-000-00000-0";

            // ArrayLists are dynamically sized, and support other
            // cool features like sorting, removing items, etc.

            System.Collections.ArrayList myArrayList = new System.Collections.ArrayList();
            myArrayList.Add(car1);
            myArrayList.Add(car2);
            myArrayList.Add(book1);
            myArrayList.Remove(book1); // if not remove the book we get a error in foreach

            foreach (object o in myArrayList)
            {
                Console.WriteLine(((Car)o).Make);
            }

            Console.WriteLine("---------------------------------------------------------");

            // Dictionaries allow you to save a key along with
            // the value, and also support cool features.
            // There are different dictionaries to choose from ...

            System.Collections.Specialized.ListDictionary myDictionary
                = new System.Collections.Specialized.ListDictionary();
            myDictionary.Add(car1.Make, car1);
            myDictionary.Add(car2.Make, car2);
            myDictionary.Add(book1.Author, book1);

            // Easy access to an element using its key
            Console.WriteLine(((Car)myDictionary["Geo"]).Model);

            // But since its not strongly typed, we can easily break it
            // by adding a different type to the dictionary ...
            // Obviously, I'm trying to retrieve a book here, and then get its ... model?
            // Console.WriteLine(((Car)myDictionary["Robert Tabor"]).Model); <-- Error

            Console.WriteLine("---------------------------------------------------------");

            List<Car> myList = new List<Car>();
            myList.Add(car1);
            myList.Add(car2);
            // myList.Add(book1); <-- error

            foreach (Car car in myList)
            {
                Console.WriteLine(car.Model);
            }

            Console.WriteLine("---------------------------------------------------------");

            Dictionary<string, Car> myDictionary2 = new Dictionary<string, Car>();
            myDictionary2.Add(car1.Make, car1);
            myDictionary2.Add(car2.Make, car2);

            Console.WriteLine(myDictionary2["Geo"].Model);

            // instance values on initialization

            string[] names = { "Bob", "Steve", "Brian", "Chuck" };

            Car car3 = new Car() { Make = "Oldsmobile", Model = "Cutlas Supreme" };
            Car car4 = new Car() { Make = "Geo", Model = "Prism" };
            Car car5 = new Car() { Make = "Nissan", Model = "Altima" };

            List<Car> myList2 = new List<Car>() {
                new Car { Make = "Oldsmobile", Model = "Cutlas Supreme"},
                new Car { Make = "Geo", Model="Prism"},
                new Car { Make = "Nissan", Model = "Altima"}
            };

            Console.ReadLine();
        }
        static void Main(string[] args)
        {
            Car car1 = new Car();
             car1.Make = "Oldsmobile";
             car1.Model = "Cutlas Supreme";

             Car car2 = new Car();
             car2.Make = "Geo";
             car2.Model = "Prism";

             Book b1 = new Book();
             b1.Author = "Robert Tabor";
             b1.Title = "Microsoft .NET XML Web Services";
             b1.ISBN = "0-000-00000-0";

             //ArrayLists are dynamically sized, and support other
             // cool features like sorting, removing items, etc.

             System.Collections.ArrayList myArrayList = new System.Collections.ArrayList();
             myArrayList.Add(car1);
             myArrayList.Add(car2);
             myArrayList.Add(b1);
             myArrayList.Remove(b1);

             foreach (object o in myArrayList)
             {
            Console.WriteLine(((Car)o).Make);//have to cast to car but then wont work with book
                                             //strongly vs. weakly typed code
             }

             // Dictionaries allow you to save a key along with
             // the value, and also support cool features.
             // There are different dictionaries to choose from ...
             /*
             System.Collections.Specialized.ListDictionary myDictionary
            = new System.Collections.Specialized.ListDictionary();

             myDictionary.Add(car1.Make, car1);
             myDictionary.Add(car2.Make, car2);
             myDictionary.Add(b1.Author, b1);

             // Easy acces to an element using its key
             Console.WriteLine(((Car)myDictionary["Geo"]).Model);

             // But since its not strongly types we can easily break it
             // by adding a different type to the dictionary ...
             // Obviously, I'm trying to retrieve a book here, and then get its ... model?
             Console.WriteLine(((Car)myDictionary["Robert Tabor"]).Model);
             */

             /*
             //Generic collection List and we give it a type Car
             List<Car> myList = new List<Car>();

             myList.Add(car1);
             myList.Add(car2);
             //myList.Add(b1);

             foreach (Car car in myList)
             {
            // No casting!
            Console.WriteLine(car.Model);
             }
             */
             /*
             Dictionary<string, Car> myDictionary = new Dictionary<string, Car>();
             myDictionary.Add(car1.Make, car1);
             myDictionary.Add(car2.Make, car2);
             //myDictionary.Add(b1.Author, b1);

             Console.WriteLine(myDictionary["Geo"].Model); //No casting!
             */
             /*
             List<Car> myList = new List<Car>() // This is all one long statement, 2 new instances of car in
                                            // a new instance of the collection
             {
            new Car {Make = "Oldsmobile", Model = "Cutlas Supreme" },
            new Car {Make = "Geo", Model = "Prism"}
             };
             */
             Console.ReadLine();
        }
Example #4
0
        static void Main(string[] args)
        {
            Car car1 = new Car();

            car1.Make  = "Oldsmobile";
            car1.Model = "Cutlas Supreme";
            car1.VIN   = "A1";

            Car car2 = new Car();

            car2.Make  = "Geo";
            car2.Model = "Prism";
            car2.VIN   = "B2";

            Book b1 = new Book();

            b1.Author = "Robert Rabor";
            b1.Title  = "Microsoft .NET XML Web Services";
            b1.ISBN   = "0-000-00000-0";

            /*
             * //ArrayLists are dynamically sized,
             * //cool features , like sorting, removing items
             * ArrayList myArrayList = new ArrayList();
             * myArrayList.Add(car1);
             * myArrayList.Add(car2);
             * myArrayList.Add(b1);
             * myArrayList.Remove(b1);
             *
             * foreach(Car car in myArrayList)
             * {
             *  Console.WriteLine(car.Make);
             * }
             */



            /*
             * //List <T>
             *
             * List<Car> myList = new List<Car>();
             *
             * myList.Add(car1);
             * myList.Add(car2);
             * //myList.Add(b1);
             * foreach(Car car in myList)
             * {
             *  Console.WriteLine(car.Model);
             * }
             */


            /*
             * //Dictionary<Tkey, TValue>
             *
             * Dictionary<string, Car> myDictionary = new Dictionary<string, Car>();
             *
             * myDictionary.Add(car1.VIN, car1);
             * myDictionary.Add(car2.VIN, car2);
             *
             * Console.WriteLine(myDictionary["B2"].Make);
             */

            //string[] names = { "Bob", "Steve", "Brian", "Chuck" };

            //Collection initializer

            List <Car> myList = new List <Car>()
            {
                new Car {
                    Make = "Oldsmobile", Model = "Cutlas Supreme", VIN = "E5"
                },
                new Car {
                    Make = "Nissan", Model = "Altima", VIN = "F6"
                }
            };

            Console.ReadLine();
        }
Example #5
0
        static void Main(string[] args)
        {
            Car car1 = new Car();

            car1.Make  = "Oldsmobile";
            car1.Model = "Cutlas Supreme";
            car1.VIN   = "A1";

            Car car2 = new Car();

            car2.Make  = "Geo";
            car2.Model = "Prism";
            car2.VIN   = "B2";

            Book b1 = new Book();

            b1.Author = "Robert Tabor";
            b1.Title  = "Microsoft .NET XML Web Services";
            b1.ISBN   = "0-000-00000-0";

            // ArrayLists are dynamically sized,
            // cool features sorting, remove items
            ArrayList myArrayList = new ArrayList();

            myArrayList.Add(car1);
            myArrayList.Add(car2);
            myArrayList.Add(b1);
            myArrayList.Remove(b1);
            foreach (Car car in myArrayList)
            {
                Console.WriteLine(car.Make);
            }

            // List<T>
            List <Car> myList = new List <Car>();

            myList.Add(car1);
            myList.Add(car2);
            //myList.Add(b1);
            foreach (Car car  in myList)
            {
                Console.WriteLine(car.Model);
            }

            // Dictionary<TKey, TValue>

            Dictionary <string, Car> myDictionary = new Dictionary <string, Car>();

            myDictionary.Add(car1.VIN, car1);
            myDictionary.Add(car2.VIN, car2);

            Console.WriteLine("Car 1 make: " + myDictionary["B2"].Make);


            string[] names = { "Bob", "Steve", "Brian", "Chuck" };

            foreach (string item in names)
            {
                Console.WriteLine("Name: " + item);
            }

            Console.WriteLine("Object initializer");
            // No need for a Constructor
            Car car3 = new Car()
            {
                Make = "BMW", Model = "750li", VIN = "C3"
            };
            Car car4 = new Car()
            {
                Make = "Toyota", Model = "4Runner", VIN = "D4"
            };

            Console.WriteLine($"Car 3:\nMake: {car3.Make}, Model: {car3.Model}, VIN: {car3.VIN}");
            Console.WriteLine($"Car 4:\nMake: {car4.Make}, Model: {car4.Model}, VIN: {car4.VIN}");

            //Collection initializer
            List <Car> myListOfCars = new List <Car>()
            {
                new Car {
                    Make = "Oldsmobile", Model = "Cutlas Supreme", VIN = "E5"
                },
                new Car {
                    Make = "Nissan", Model = "Altima", VIN = "F6"
                }
            };

            Console.ReadLine();
        }