//Classes and Delegates are always reference types //Structs are value types static void Main(string[] args) { Module03BasicTypesAndConstructs.Coffee firstBean = new Module03BasicTypesAndConstructs.Coffee(5, "Good Beans", "My Place"); firstBean.OutOfBeans += new Inventory().HandleOutOfBeans; DrinksMachine dm = new DrinksMachine { Age = 2, Model = "BeanSmasher 3000", Make = "Fourth Coffee" }; for (int x = 0; x < 4; x++) { dm.MakeCappuccino(ref firstBean); } DrinksMachine dm2 = new DrinksMachine(2); DrinksMachine dm3 = new DrinksMachine("Fourth Coffee", "BeanDestroyer 3000"); DrinksMachine dm4 = new DrinksMachine(3, "Fourth Coffee", "BeanToaster Turbo"); //Boxing is converting a value type to a reference type by wrapping it in an object int i = 100; object obj = i; // Unboxing is converting a reference type to a value type and you must explicitly cast the variable back to its original type int j; j = (int)obj; double weightInKilos = 80; double weightInPounds = Conversions.KilosToPounds(weightInKilos); //Using IComparer implentation to sort coffee by rating rather than Variety (default set by coffee class IComparable implentation) //Coffee Class (rather than coffee struct above) Coffee coffeenew1 = new Coffee(); coffeenew1.AverageRating = 4.5; Coffee coffeenew2 = new Coffee(); coffeenew2.AverageRating = 8.1; Coffee coffeenew3 = new Coffee(); coffeenew3.AverageRating = 7.1; //Add the instances to arraylist ArrayList coffeList = new ArrayList(); coffeList.Add(coffeenew1); coffeList.Add(coffeenew2); coffeList.Add(coffeenew3); //Sort arraylist by average rating(passed custom Icomparer implentation) coffeList.Sort(new CoffeeRatingComparer()); foreach (Coffee drink in coffeList) { Console.WriteLine(drink.AverageRating); } Console.ReadLine(); }
static void Main(string[] args) { #region Enums //Set enum variable by name or value Day favoriteDay = Day.Friday; // is equivalent to Day favoriteDay1 = (Day)6; Console.WriteLine($"{favoriteDay} via Day.Friday and {favoriteDay1} via (Day)6."); #endregion #region structs //instantiating a struct without constructor Coffee coffee1 = new Coffee { Strength = 3, Bean = "Arabica", CountryOfOrigin = "Columbia" }; Coffee coffee2 = new Coffee(5, "Antigua Volcanic", "Guetemala"); //Accessing a public array in a struct PublicMenu myMenu = new PublicMenu("cocoa"); string firstDrink = myMenu.beverages[0]; Console.WriteLine(firstDrink); //Accessing a value in a private array via indexer Menu myPrivateMenu = new Menu("YooHoo", "Screwdriver"); string firstPrivateDrink = myPrivateMenu[0]; int numberOfChoices = myPrivateMenu.Length; Console.WriteLine($"The first private drink is {firstPrivateDrink} which I accessed via an indexer and the number of beverages to choose is {numberOfChoices}."); #endregion #region Collections //Collections are multiple items of the same time stored in a variable //List store linear collections of items //Dictionary store collections of key/value pairs //Queue store items in a FIFO collection //Stack store items in a LIFO collection //Core Operations of collections: Add items, remove items, retrieve specific items, count items, iterate through collection(one at a time) #region List collection ArrayList beverages = new ArrayList(); //Items are implicitly cast to the object type when you add them beverages.Add(coffee1); beverages.Add(coffee2); //retrieve items by index //Items must be explicitly cast back to their original type Coffee firstCoffee = (Coffee)beverages[0]; Coffee secondCoffee = (Coffee)beverages[1]; Console.WriteLine(firstCoffee + " " + secondCoffee); //foreach loop to iterate over collection foreach (Coffee c in beverages) { Console.WriteLine(c.CountryOfOrigin); } #endregion #region Dictionary Collection //Create a new hashtable collection Hashtable ingredients = new Hashtable(); // Add some key/value pairs to the collection ingredients.Add("Cafe au Lait", "Coffee, Milk"); ingredients.Add("Cafe Mocha", "Coffee, Milk, Chocolate"); ingredients.Add("Cappuccino", "Coffee, Milk, Foam"); ingredients.Add("Irish Coffee", "Coffee, Whiskey, Cream, Sugar"); ingredients.Add("Macchiato", "Coffee, Milk, Foam"); //Check whether a key exists. if (ingredients.ContainsKey("Cafe Mocha")) { // Retrieve the value associated with a key. Console.WriteLine("the ingredients of a cafe mocha are: {0}", ingredients["Cafe Mocha"]); } // dictionary classes actually contain two enumerable collections so you can iterate over either of these collections // although it is usually the keys you iterate over to retrieve the values foreach (string key in ingredients.Keys) { // For Each key, retrieve the value associated with the key. Console.WriteLine("The ingredients of a {0} are {1}", key, ingredients[key]); } #endregion #region Querying a Collection //LINQ expressions // from <variable names> in <data source> // where <selection criteria> // orderby <result ordering criteria> // select <variable names> // Can also use: FirstOrDefault, Last, Max, and Min after the query above (use dot notation on the variable name(s) // Create a new hashtable with drinks and prices Hashtable prices = new Hashtable(); prices.Add("Cafe au Lait", 1.99M); prices.Add("Caffe Americano", 1.89M); prices.Add("Cafe Mocha", 2.99M); prices.Add("Cappuccino", 2.49M); prices.Add("Espresso", 1.49M); prices.Add("Espresso Romana", 1.59M); prices.Add("English Tea", 1.69M); prices.Add("Juice", 2.89M); // Select all the drinks that cost les than $2.00 and order them by cost. var bargains = from string drink in prices.Keys where (Decimal)prices[drink] < 2.00M orderby prices[drink] ascending select drink; // Display the results foreach (string bargain in bargains) { Console.WriteLine("{0} costs {1}", bargain, prices[bargain]); } #endregion #endregion #region Handling Events // Delegates are reference to Methods // They can be saved as field and passed as parameter //They allow one to execute code that is provided from external sources //Delegates can be executed exactly like methods //Delegates first parameter is the object that raised the event //Second parameter is the event arguments that much be an instance of EventArgs or an instance of a class that derives from EventArgs // events wrap delegates like a property wraps a field // Allow the class to notify other consumers of actions performed within it. // An event may only be raised by it's containing class Inventory myInventory = new Inventory(); coffee2.OutOfBeans += myInventory.HandleOutOfBeans; for (int i = 0; i < 9; i++) { coffee2.MakeCoffee(); } #endregion Console.ReadLine(); }