/** * * Method Name: UseObjectInitializer * Parameters: none * Return value: none * * Use Object Initializer * * Instantiate the following objects: * 1. $1.49 Wheat Bread * 2. Dictionary of numbers: 1 is One, 2 is Two, 3 is Three * 3. Dictionary of US States: NH is New Hampshire, NY is New York, NJ is New Jersey * */ public void UseObjectInitializer() { Console.WriteLine("\n\t FinalExam.UseObjectInitializer()..."); Console.WriteLine(" (1)Instantiate the $1.49 Wheat Bread."); MyBread TracyBread = new MyBread { Id = 1, Name = "Wheat Bread", Price = 1.49 }; Console.WriteLine($"{TracyBread.Price} {TracyBread.Name} "); Console.WriteLine(" (2)Instantiate dictionary of numbers: 1 is One, 2 is Two, 3 is Three "); Dictionary <int, string> numberDictionary = new Dictionary <int, string>(); Console.WriteLine("Dictionary of numbers:"); numberDictionary.Add(1, "one"); numberDictionary.Add(2, "two"); numberDictionary.Add(3, "three"); foreach (KeyValuePair <int, string> n in numberDictionary) { Console.WriteLine(n.Key + " is " + n.Value); } Console.WriteLine(" (3)Instantiate dictionary of US States: NH is New Hampshire, NY is New York, NJ is New Jersey"); Dictionary <string, string> stateDictionary = new Dictionary <string, string>(); Console.WriteLine("Dictionary of states:"); stateDictionary.Add("NH", "New Hampshire"); stateDictionary.Add("NY", "New York"); stateDictionary.Add("NJ", "New Jersey"); foreach (KeyValuePair <string, string> n in stateDictionary) { Console.WriteLine(n.Key + " is " + n.Value); } Console.WriteLine("\n\t FinalExam.UseObjectInitializer()...done!"); }
/** * * Method Name: UseObjectInitializer * Parameters: none * Return value: none * * Use Object Initializer * * Instantiate the following objects: * 1. $1.49 Wheat Bread * 2. Dictionary of numbers: 1 is One, 2 is Two, 3 is Three * 3. Dictionary of US States: NH is New Hampshire, NY is New York, NJ is New Jersey * */ public void UseObjectInitializer() { Console.WriteLine("\n\t FinalExam.UseObjectInitializer()..."); MyBread bread = new MyBread(1, 1.49, "Wheat Bread"); Dictionary <int, string> numbers = new Dictionary <int, string>(); Dictionary <string, string> states = new Dictionary <string, string>(); numbers.Add(1, "One"); numbers.Add(2, "Two"); numbers.Add(3, "Three"); states.Add("NH", "New Hampshire"); states.Add("NY", "New York"); states.Add("NJ", "New Jersey"); Console.WriteLine("\n\t FinalExam.UseObjectInitializer()...done!"); }