using System.Reflection; namespace ConsoleAdvancedTopics { class Program { /* // Option 1 - Use a single Thread static void Main(string[] args) */ // Option 2 - Use Multi-Threading and Async Tasks static async Task Main(string[] args) { // start of the Program Class // Create a user account using name and email // List of emails string[] emails = { "bob@1234.com", "bill", "mary@gmail.123" }; // Instance of the Regex Class RegularExpressionEmail emailValidator = new RegularExpressionEmail(); // Call the validator and pass the emails list emailValidator.ValidateEmails(emails); // Get the Order from the Carts using Lambdas and LINQ "Order Class" // Create a list of sample orders List orders = new List { new Order { Id = 2001, CustomerName = "Alice", TotalAmount = 100.50m, OrderDate = new DateTime(2024, 8, 2) }, new Order { Id = 2002, CustomerName = "Bob", TotalAmount = 200.50m, OrderDate = new DateTime(2024, 7, 4) }, new Order { Id = 2003, CustomerName = "Alice", TotalAmount = 50.50m, OrderDate = new DateTime(2024, 8, 12) }, new Order { Id = 2004, CustomerName = "Mary", TotalAmount = 70.50m, OrderDate = new DateTime(2024, 6, 21) } }; // List // Example 1 Lambda with LINQ filter WHERE var OrdersOver100 = orders.Where( // Lambda with LINQ // Lambda params => op; // LINQ Where relational operator > < >= <= != o => o.TotalAmount > 100 ); // var // results of Lambda Expression with LINQ Console.WriteLine("=== Results of Orders Over $100 Query ==="); foreach (var order in OrdersOver100) // filtered list of orders > 100 { Console.WriteLine($"Order id {order.Id}, Order Total: {order.TotalAmount}"); } // Example 2 Use a Sum aggregate with Lambda. Sum is coming from LINQ var totalRevenue = orders.Sum( // LINQ // Lambda o => o.TotalAmount ); // Results Console.WriteLine($"\nTotal Revenue: ${totalRevenue}\n"); // Example 3 GroupBy from LINQ with Lambda, and add Count aggregate LINQ var ordersByCustomer = orders.GroupBy(o => o.CustomerName); // List of Grouped Customers autoApply a key: value for each customer // Results Console.WriteLine($"Count of Orders by Customer"); foreach (var group in ordersByCustomer) { Console.WriteLine($"{group.Key}: {group.Count()} order(s), Group Total: ${group.Sum(o => o.TotalAmount)}"); } // Product Class as Properties of the Class // No Product Class Type Exists / Neither Instance Of // There is no Product Type // Reflection Example Type productType = typeof( Product ); // replaces/ in lue of "new instance of" // Get properties using reflection PropertyInfo[] properties = productType.GetProperties(); // Results Console.WriteLine("\n===Product Properties==="); foreach (var property in properties) { Console.WriteLine($"{property.Name} Type: {property.PropertyType} "); } // Create an instance using reflection // !new Class() instead Activator.CreateInstance(PropertyType) object productInstance = Activator.CreateInstance( productType ); // Setting the value of the Property // No getter ot setter for value of the instance because there is no local instance with a set type (int, string, decimal) productType.GetProperty("Id").SetValue(productInstance, 3001); productType.GetProperty("ProductName").SetValue(productInstance, "Widget 1"); productType.GetProperty("Price").SetValue(productInstance, 252.52m); // Display the Product Property Info using the Property Method MethodInfo displayMethod = productType.GetMethod("DisplayInfoFromProperty"); displayMethod.Invoke(productInstance, null); // Product Class as a New Instance of the Class Program // using a class instance Product product = new Product(); // locally assigned var product.DisplayInfoFromClass(); // no values // Create an instance for a customer and add 2 orders using ( Customers customer1 = new Customers(1, "Bob Smith") ) { // save 2 new orders customer1.SaveOrder("Order #1001"); customer1.SaveOrder("Order #1002"); } // using when completed exit code 0 auto runs Dispose() AUTOMATIC with using keyword Customers customer2 = new Customers(2, "Jane Doe"); customer2.SaveOrder("Order # 1003"); // at this point customer 2 is still in memory customer2.Dispose(); // next time Dispose runs this object will be marked as "to be disposed" MANUALLY Customers customer3 = new Customers(3, "Harry Stiles"); customer3.SaveOrder("Order #3001"); // at this point customer3 relies on GC Finalizer to run periodically OR when memory is low. // Set and Get the CC Info CreditCardProcessorTuple processor = new CreditCardProcessorTuple(); var cardInfo = processor.GetCreditCardInfo(); // Call the Display CC Info using object Option 1 processor.DisplayCreditCardInfo(cardInfo); // Deconstruct the object "unpacking" Option 2 var (number, expdate, cvc, zipcode) = cardInfo; Console.WriteLine($"Card Number: {number}"); Console.WriteLine($"Card Exp: {expdate}"); Console.WriteLine($"Card CVC: {cvc}"); Console.WriteLine($"Zip Code: {zipcode}"); // Notify an event happened Publisher publisher = new Publisher(); Subcriber subcriber1 = new Subcriber(); Subcriber subcriber2 = new Subcriber(); // Subscribe to the event using the delegate type publisher.MessagePublished += subcriber1.HandleMessage; publisher.MessagePublished += subcriber2.HandleMessage; // Publish a message // if statement approved publisher.PublishMessage("Processed Credit Card API Approved"); // else declined // loop back to the credit card inputs // Credit Card API has approved the purchase // OrderProcessingProcess Main Chapter 12 //// Shipping API sub Thread Task //// Warehourse API sub Thread Task // Create an instance of the class that has our Parent Task ProcessingOrderAfterApproval p = new ProcessingOrderAfterApproval(); // Run the ProcessOrder Parent Task // this will run both the public parent and the 2 private sub Tasks await RunInProgram(p); } // Main // Create an orderid and a task to run static async Task RunInProgram(ProcessingOrderAfterApproval p) { int orderID = 1234567; await p.ProcessOrder(orderID); } } // class Program } // namespace