├── .DS_Store ├── .gitignore ├── CSharp9Cookbook ├── .DS_Store ├── .vscode │ ├── launch.json │ └── tasks.json ├── CSharp9Cookbook.sln ├── Chapter01 │ ├── Section-1-1 │ │ ├── DeploymentProcess.cs │ │ ├── DeploymentReport.txt │ │ ├── Program.cs │ │ └── Section-1-1.csproj │ ├── Section-1-10 │ │ ├── DeploymentBuilder.cs │ │ ├── DeploymentService.cs │ │ ├── Program.cs │ │ └── Section-1-10.csproj │ ├── Section-1-2 │ │ ├── DeploymentArtifacts.cs │ │ ├── DeploymentRepository.cs │ │ ├── DeploymentService.cs │ │ ├── IDeploymentService.cs │ │ ├── Program.cs │ │ └── Section-1-2.csproj │ ├── Section-1-3 │ │ ├── IValidatorFactory.cs │ │ ├── Program.cs │ │ ├── Section-1-3.csproj │ │ ├── ThirdPartyDeploymentService.cs │ │ └── ValidatorFactory.cs │ ├── Section-1-4 │ │ ├── DeploymentManagementBase.cs │ │ ├── DeploymentManager1.cs │ │ ├── DeploymentManager2.cs │ │ ├── DeploymentPlugin1.cs │ │ ├── DeploymentPlugin2.cs │ │ ├── IDeploymentPlugin.cs │ │ ├── Program.cs │ │ └── Section-1-4.csproj │ ├── Section-1-5 │ │ ├── MultiLayer │ │ │ ├── EnterpriseApp.CmdLine │ │ │ │ ├── EnterpriseApp.CmdLine.csproj │ │ │ │ ├── Program.cs │ │ │ │ └── View │ │ │ │ │ ├── Menu.cs │ │ │ │ │ └── SignIn.cs │ │ │ ├── EnterpriseApp.Data │ │ │ │ ├── EnterpriseApp.Data.csproj │ │ │ │ └── GreetingRepository.cs │ │ │ └── EnterpriseApp │ │ │ │ ├── EnterpriseApp.csproj │ │ │ │ └── Greeting.cs │ │ ├── SeparateData │ │ │ ├── SystemApp.Console │ │ │ │ ├── Model │ │ │ │ │ └── Greeting.cs │ │ │ │ ├── Program.cs │ │ │ │ ├── SystemApp.Console.csproj │ │ │ │ └── View │ │ │ │ │ ├── Menu.cs │ │ │ │ │ └── SignIn.cs │ │ │ └── SystemApp.Data │ │ │ │ ├── GreetingRepository.cs │ │ │ │ └── SystemApp.Data.csproj │ │ └── SingleProject │ │ │ └── SimpleApp │ │ │ ├── Data │ │ │ └── GreetingRepository.cs │ │ │ ├── Model │ │ │ └── Greeting.cs │ │ │ ├── Program.cs │ │ │ ├── SimpleApp.csproj │ │ │ └── View │ │ │ ├── Menu.cs │ │ │ └── SignIn.cs │ ├── Section-1-6 │ │ ├── DeploymentService.cs │ │ ├── Program.cs │ │ ├── Section-1-6.csproj │ │ └── ValidationStatus.cs │ ├── Section-1-7 │ │ ├── CircularQueueGeneric.cs │ │ ├── CircularQueueObjects.cs │ │ ├── Deployment.cs │ │ ├── HealthChecksGeneric.cs │ │ ├── HealthChecksObjects.cs │ │ ├── Program.cs │ │ └── Section-1-7.csproj │ ├── Section-1-8 │ │ ├── DeploymentService1.cs │ │ ├── DeploymentService2.cs │ │ ├── IDeploymentService.cs │ │ ├── Program.cs │ │ ├── Section-1-8.csproj │ │ ├── ThirdPartyDeploymentAdapter.cs │ │ └── ThirdPartyDeploymentService.cs │ └── Section-1-9 │ │ ├── DeploymentService.cs │ │ ├── DeploymentValidationException.cs │ │ ├── Program.cs │ │ ├── Section-1-9.csproj │ │ └── ValidationFailureReason.cs ├── Chapter02 │ ├── Section-02-01 │ │ ├── Program.cs │ │ └── Section-02-01.csproj │ ├── Section-02-02 │ │ ├── Invoice.txt │ │ ├── Program.cs │ │ └── Section-02-02.csproj │ ├── Section-02-03 │ │ ├── Program.cs │ │ └── Section-02-03.csproj │ ├── Section-02-04 │ │ ├── BankInvoice.cs │ │ ├── EnterpriseInvoice.cs │ │ ├── GovernmentInvoice.cs │ │ ├── IInvoice.cs │ │ ├── Program.cs │ │ └── Section-02-04.csproj │ ├── Section-02-05 │ │ ├── Invoice.cs │ │ ├── Program.cs │ │ └── Section-02-05.csproj │ ├── Section-02-06 │ │ ├── Program.cs │ │ └── Section-02-06.csproj │ ├── Section-02-07 │ │ ├── Program.cs │ │ └── Section-02-07.csproj │ ├── Section-02-08 │ │ ├── IInvoiceRepository.cs │ │ ├── InvoiceCategory.cs │ │ ├── InvoiceRepository.cs │ │ ├── Program.cs │ │ └── Section-02-08.csproj │ ├── Section-02-09 │ │ ├── IInvoiceRepository.cs │ │ ├── InvoiceCategory.cs │ │ ├── InvoiceRepository.cs │ │ ├── Program.cs │ │ └── Section-02-09.csproj │ └── Section-02-10 │ │ ├── Program.cs │ │ └── Section-02-10.csproj ├── Chapter03 │ ├── Section-03-01 │ │ └── OrdersLibrary │ │ │ ├── CustomerType.cs │ │ │ ├── Order.cs │ │ │ └── OrdersLibrary.csproj │ ├── Section-03-02 │ │ ├── CompanyOrder.cs │ │ ├── CustomerOrder.cs │ │ ├── IOrder.cs │ │ ├── Program.cs │ │ └── Section-03-02.csproj │ ├── Section-03-03 │ │ ├── Program.cs │ │ └── Section-03-03.csproj │ ├── Section-03-04 │ │ ├── OrderLibraryNonNull.cs │ │ ├── OrderLibraryWithNull.cs │ │ ├── Program.cs │ │ └── Section-03-04.csproj │ ├── Section-03-05 │ │ ├── Delivery.cs │ │ ├── Order.cs │ │ ├── Program.cs │ │ └── Section-03-05.csproj │ ├── Section-03-06 │ │ ├── Order.cs │ │ ├── Program.cs │ │ └── Section-03-06.csproj │ ├── Section-03-07 │ │ ├── OrderOrchestrator.cs │ │ ├── Orders.cs │ │ ├── Program.cs │ │ └── Section-03-07.csproj │ ├── Section-03-08 │ │ ├── Program.cs │ │ └── Section-03-08.csproj │ ├── Section-03-09 │ │ ├── Program.cs │ │ └── Section-03-09.csproj │ └── Section-03-10 │ │ ├── Program.cs │ │ └── Section-03-10.csproj ├── Chapter04 │ ├── Section-04-01 │ │ ├── InMemoryContext.cs │ │ ├── Program.cs │ │ ├── SalesPerson.cs │ │ └── Section-04-01.csproj │ ├── Section-04-02 │ │ ├── InMemoryContext.cs │ │ ├── Product.cs │ │ ├── Program.cs │ │ ├── SalesPerson.cs │ │ └── Section-04-02.csproj │ ├── Section-04-03 │ │ ├── InMemoryContext.cs │ │ ├── Product.cs │ │ ├── Program.cs │ │ ├── SalesPerson.cs │ │ └── Section-04-03.csproj │ ├── Section-04-04 │ │ ├── InMemoryContext.cs │ │ ├── Program.cs │ │ ├── SalesPerson.cs │ │ └── Section-04-04.csproj │ ├── Section-04-05 │ │ ├── InMemoryContext.cs │ │ ├── Program.cs │ │ ├── SalesPerson.cs │ │ └── Section-04-05.csproj │ ├── Section-04-06 │ │ ├── InMemoryContext.cs │ │ ├── Program.cs │ │ ├── SalesPerson.cs │ │ └── Section-04-06.csproj │ ├── Section-04-07 │ │ ├── InMemoryContext.cs │ │ ├── Program.cs │ │ ├── SalesPerson.cs │ │ └── Section-04-07.csproj │ ├── Section-04-08 │ │ ├── InMemoryContext.cs │ │ ├── Program.cs │ │ ├── SalesPerson.cs │ │ └── Section-04-08.csproj │ ├── Section-04-09 │ │ ├── CookbookExtensions.cs │ │ ├── InMemoryContext.cs │ │ ├── Program.cs │ │ ├── SalesPerson.cs │ │ └── Section-04-09.csproj │ └── Section-04-10 │ │ ├── InMemoryContext.cs │ │ ├── Program.cs │ │ ├── SalesPerson.cs │ │ └── Section-04-10.csproj ├── Chapter05 │ ├── .DS_Store │ ├── Section-05-01 │ │ ├── ColumnAttribute.cs │ │ ├── InventoryItem.cs │ │ ├── Program.cs │ │ ├── Report.cs │ │ └── Section-05-01.csproj │ ├── Section-05-02 │ │ ├── ColumnAttribute.cs │ │ ├── ColumnDetail.cs │ │ ├── InventoryItem.cs │ │ ├── Program.cs │ │ ├── Report.cs │ │ └── Section-05-02.csproj │ ├── Section-05-03 │ │ ├── ColumnAttribute.cs │ │ ├── ColumnDetail.cs │ │ ├── GeneratorBase.cs │ │ ├── HtmlGenerator.cs │ │ ├── InventoryItem.cs │ │ ├── MarkdownGenerator.cs │ │ ├── Program.cs │ │ ├── Report.cs │ │ ├── ReportType.cs │ │ └── Section-05-03.csproj │ ├── Section-05-04 │ │ ├── ColumnAttribute.cs │ │ ├── ColumnDetail.cs │ │ ├── InventoryItem.cs │ │ ├── Program.cs │ │ ├── Report.cs │ │ └── Section-05-04.csproj │ ├── Section-05-05 │ │ ├── ColumnAttribute.cs │ │ ├── ColumnDetail.cs │ │ ├── Inventory.cs │ │ ├── InventoryItem.cs │ │ ├── Program.cs │ │ ├── Report.cs │ │ └── Section-05-05.csproj │ ├── Section-05-06 │ │ ├── ColumnAttribute.cs │ │ ├── ColumnDetail.cs │ │ ├── ExcelDynamicGenerator.cs │ │ ├── ExcelTypedGenerator.cs │ │ ├── GeneratorBase.cs │ │ ├── HtmlGenerator.cs │ │ ├── InventoryItem.cs │ │ ├── MarkdownGenerator.cs │ │ ├── Program.cs │ │ ├── Report.cs │ │ ├── ReportType.cs │ │ └── Section-05-06.csproj │ ├── Section-05-07 │ │ ├── ColumnAttribute.cs │ │ ├── ColumnDetail.cs │ │ ├── DynamicLog.cs │ │ ├── LogEntry.cs │ │ ├── Program.cs │ │ ├── Report.cs │ │ └── Section-05-07.csproj │ ├── Section-05-08 │ │ ├── ColumnAttribute.cs │ │ ├── ColumnDetail.cs │ │ ├── LogEntry.cs │ │ ├── Program.cs │ │ ├── Report.cs │ │ └── Section-05-08.csproj │ ├── Section-05-09 │ │ ├── ColumnAttribute.cs │ │ ├── ColumnDetail.cs │ │ ├── Program.cs │ │ ├── Report.cs │ │ ├── Section-05-09.csproj │ │ ├── Semantic.py │ │ └── Tweet.cs │ ├── Section-05-10-old │ │ ├── .DS_Store │ │ ├── ColumnAttribute.cs │ │ ├── ColumnDetail.cs │ │ ├── InventoryItem.cs │ │ ├── MainApp.py │ │ ├── Report.cs │ │ └── Section-05-10.csproj │ └── Section-05-10 │ │ ├── ColumnAttribute.cs │ │ ├── ColumnDetail.cs │ │ ├── InventoryItem.cs │ │ ├── MainApp.py │ │ ├── Properties │ │ └── AssemblyInfo.cs │ │ ├── Report.cs │ │ └── Section-05-10.csproj ├── Chapter06 │ ├── .DS_Store │ ├── Section-06-01 │ │ ├── .DS_Store │ │ ├── CheckoutService.cs │ │ ├── Program.cs │ │ └── Section-06-01.csproj │ ├── Section-06-02 │ │ ├── CheckoutService.cs │ │ ├── Program.cs │ │ └── Section-06-02.csproj │ ├── Section-06-03 │ │ ├── CheckoutRequest.cs │ │ ├── CheckoutService.cs │ │ ├── CheckoutStream.cs │ │ ├── Program.cs │ │ └── Section-06-03.csproj │ ├── Section-06-04 │ │ ├── CheckoutService.cs │ │ ├── Program.cs │ │ └── Section-06-04.csproj │ ├── Section-06-05 │ │ ├── CheckoutRequest.cs │ │ ├── CheckoutRequestProgress.cs │ │ ├── CheckoutService.cs │ │ ├── CheckoutStream.cs │ │ ├── Program.cs │ │ └── Section-06-05.csproj │ ├── Section-06-06 │ │ ├── CheckoutService.cs │ │ ├── Program.cs │ │ └── Section-06-06.csproj │ ├── Section-06-07 │ │ ├── CheckoutService.cs │ │ ├── Program.cs │ │ └── Section-06-07.csproj │ ├── Section-06-08 │ │ ├── CheckoutService.cs │ │ ├── Program.cs │ │ └── Section-06-08.csproj │ ├── Section-06-09 │ │ ├── CheckoutRequest.cs │ │ ├── CheckoutRequestProgress.cs │ │ ├── CheckoutService.cs │ │ ├── CheckoutStream.cs │ │ ├── Program.cs │ │ └── Section-06-09.csproj │ └── Section-06-10 │ │ ├── CheckoutRequest.cs │ │ ├── CheckoutRequestProgress.cs │ │ ├── CheckoutService.cs │ │ ├── CheckoutStream.cs │ │ ├── ConsoleLogger.cs │ │ ├── ILogger.cs │ │ ├── Program.cs │ │ └── Section-06-10.csproj ├── Chapter07 │ ├── .DS_Store │ ├── Section-07-01 │ │ ├── Program.cs │ │ └── Section-07-01.csproj │ ├── Section-07-02 │ │ ├── Crypto.cs │ │ ├── Program.cs │ │ └── Section-07-02.csproj │ ├── Section-07-03 │ │ ├── Program.cs │ │ └── Section-07-03.csproj │ ├── Section-07-04 │ │ ├── Program.cs │ │ ├── PurchaseItem.cs │ │ ├── PurchaseOrder.cs │ │ ├── PurchaseOrderService.cs │ │ ├── PurchaseOrderStatus.cs │ │ └── Section-07-04.csproj │ ├── Section-07-05 │ │ ├── Program.cs │ │ ├── PurchaseItem.cs │ │ ├── PurchaseOrder.cs │ │ ├── PurchaseOrderService.cs │ │ ├── PurchaseOrderStatus.cs │ │ ├── PurchaseOrderStatusConverter.cs │ │ ├── Section-07-05.csproj │ │ └── SnakeCaseNamingPolicy.cs │ ├── Section-07-06 │ │ ├── JsonConversionExtensions.cs │ │ ├── Program.cs │ │ ├── PurchaseItem.cs │ │ ├── PurchaseOrder.cs │ │ ├── PurchaseOrderService.cs │ │ ├── PurchaseOrderStatus.cs │ │ └── Section-07-06.csproj │ ├── Section-07-07 │ │ ├── Program.cs │ │ ├── PurchaseItem.cs │ │ ├── PurchaseOrder.cs │ │ ├── PurchaseOrderStatus.cs │ │ └── Section-07-07.csproj │ ├── Section-07-08 │ │ ├── Program.cs │ │ ├── PurchaseItem.cs │ │ ├── PurchaseOrder.cs │ │ ├── PurchaseOrderStatus.cs │ │ └── Section-07-08.csproj │ ├── Section-07-09 │ │ ├── Program.cs │ │ ├── Section-07-09.csproj │ │ └── Url.cs │ └── Section-07-10 │ │ ├── Program.cs │ │ ├── Section-07-10.csproj │ │ └── StringExtensions.cs ├── Chapter08 │ ├── Section-08-01 │ │ ├── BronzeSchedule.cs │ │ ├── GoldSchedule.cs │ │ ├── IRoomSchedule.cs │ │ ├── Program.cs │ │ ├── Section-08-01.csproj │ │ └── SilverSchedule.cs │ ├── Section-08-02 │ │ ├── Program.cs │ │ ├── Scheduler.cs │ │ └── Section-08-02.csproj │ ├── Section-08-03 │ │ ├── BronzeSchedule.cs │ │ ├── GoldSchedule.cs │ │ ├── IRoomSchedule.cs │ │ ├── Program.cs │ │ ├── ScheduleType.cs │ │ ├── Scheduler.cs │ │ ├── Section-08-03.csproj │ │ └── SilverSchedule.cs │ ├── Section-08-04 │ │ ├── Program.cs │ │ ├── Room.cs │ │ ├── ScheduleType.cs │ │ └── Section-08-04.csproj │ ├── Section-08-05 │ │ ├── Program.cs │ │ ├── Room.cs │ │ ├── ScheduleType.cs │ │ └── Section-08-05.csproj │ ├── Section-08-06 │ │ ├── Program.cs │ │ ├── Room.cs │ │ ├── ScheduleType.cs │ │ └── Section-08-06.csproj │ ├── Section-08-07 │ │ ├── BronzeSchedule.cs │ │ ├── GoldSchedule.cs │ │ ├── IRoomSchedule.cs │ │ ├── Program.cs │ │ ├── Section-08-07.csproj │ │ └── SilverSchedule.cs │ ├── Section-08-08 │ │ ├── BronzeSchedule.cs │ │ ├── Customer.cs │ │ ├── GoldSchedule.cs │ │ ├── IRoomSchedule.cs │ │ ├── Program.cs │ │ ├── Section-08-08.csproj │ │ └── SilverSchedule.cs │ ├── Section-08-09 │ │ ├── Customer.cs │ │ ├── Program.cs │ │ └── Section-08-09.csproj │ └── Section-08-10 │ │ ├── BronzeCustomer.cs │ │ ├── BronzeSchedule.cs │ │ ├── Customer.cs │ │ ├── GoldCustomer.cs │ │ ├── GoldSchedule.cs │ │ ├── IRoomSchedule.cs │ │ ├── Program.cs │ │ ├── Section-08-10.csproj │ │ ├── SilverCustomer.cs │ │ └── SilverSchedule.cs ├── Chapter09 │ ├── Section-09-01 │ │ ├── AnotherFile.cs │ │ ├── Program.cs │ │ └── Section-09-01.csproj │ ├── Section-09-02 │ │ ├── Address.cs │ │ ├── Program.cs │ │ └── Section-09-02.csproj │ ├── Section-09-03 │ │ ├── Address.cs │ │ ├── Program.cs │ │ └── Section-09-03.csproj │ ├── Section-09-04 │ │ ├── Program.cs │ │ └── Section-09-04.csproj │ ├── Section-09-05 │ │ ├── Program.cs │ │ └── Section-09-05.csproj │ ├── Section-09-06 │ │ ├── AddressBase.cs │ │ ├── MailingAddress.cs │ │ ├── Program.cs │ │ ├── Section-09-06.csproj │ │ └── ShippingAddress.cs │ ├── Section-09-07 │ │ ├── AddressBase.cs │ │ ├── Communications.cs │ │ ├── DeliveryBase.cs │ │ ├── MailingAddress.cs │ │ ├── Program.cs │ │ ├── Section-09-07.csproj │ │ ├── Shipping.cs │ │ └── ShippingAddress.cs │ ├── Section-09-08 │ │ ├── Address.cs │ │ ├── AddressExtensions.cs │ │ ├── Program.cs │ │ └── Section-09-08.csproj │ ├── Section-09-09 │ │ ├── Address.cs │ │ ├── AddressService.cs │ │ ├── Program.cs │ │ └── Section-09-09.csproj │ └── Section-09-10 │ │ ├── AddressManager │ │ ├── AddressManager.csproj │ │ └── Program.cs │ │ └── AddressUtilities │ │ ├── Address.cs │ │ ├── AddressRepository.cs │ │ ├── AddressService.cs │ │ ├── AddressUtilities.csproj │ │ ├── IAddressRepository.cs │ │ └── Initializer.cs └── OrdersLibrary.Test │ ├── OrderTests.cs │ └── OrdersLibrary.Test.csproj ├── LICENSE.md └── README.md /.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JoeMayo/csharp-nine-cookbook/a22b77eb3e472a847035297dac9d4594a96f458f/.DS_Store -------------------------------------------------------------------------------- /CSharp9Cookbook/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JoeMayo/csharp-nine-cookbook/a22b77eb3e472a847035297dac9d4594a96f458f/CSharp9Cookbook/.DS_Store -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter01/Section-1-1/DeploymentProcess.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.IO; 3 | 4 | namespace Section_1_1 5 | { 6 | public class DeploymentProcess : IDisposable 7 | { 8 | bool disposed; 9 | 10 | readonly StreamWriter report = new StreamWriter("DeploymentReport.txt"); 11 | 12 | public bool CheckStatus() 13 | { 14 | report.WriteLine($"{DateTime.Now} Application Deployed."); 15 | 16 | return true; 17 | } 18 | 19 | protected virtual void Dispose(bool disposing) 20 | { 21 | if (!disposed) 22 | { 23 | if (disposing) 24 | { 25 | // disposal of purely managed resources goes here 26 | } 27 | 28 | report?.Close(); 29 | disposed = true; 30 | } 31 | } 32 | 33 | ~DeploymentProcess() 34 | { 35 | Dispose(disposing: false); 36 | } 37 | 38 | public void Dispose() 39 | { 40 | Dispose(disposing: true); 41 | GC.SuppressFinalize(this); 42 | } 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter01/Section-1-1/DeploymentReport.txt: -------------------------------------------------------------------------------- 1 | 7/19/2021 9:10:36 PM Application Deployed. 2 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter01/Section-1-1/Program.cs: -------------------------------------------------------------------------------- 1 | namespace Section_1_1 2 | { 3 | class Program 4 | { 5 | static void Main(string[] args) 6 | { 7 | using (var deployer = new DeploymentProcess()) 8 | { 9 | deployer.CheckStatus(); 10 | } 11 | } 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter01/Section-1-1/Section-1-1.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | net7.0 6 | Section_1_1 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter01/Section-1-10/DeploymentBuilder.cs: -------------------------------------------------------------------------------- 1 | namespace Section_1_10 2 | { 3 | public class DeploymentBuilder 4 | { 5 | DeploymentService service = new DeploymentService(); 6 | 7 | public DeploymentBuilder SetStartDelay(int delay) 8 | { 9 | service.StartDelay = delay; 10 | return this; 11 | } 12 | 13 | public DeploymentBuilder SetErrorRetries(int retries) 14 | { 15 | service.ErrorRetries = retries; 16 | return this; 17 | } 18 | 19 | public DeploymentBuilder SetReportFormat(string format) 20 | { 21 | service.ReportFormat = format; 22 | return this; 23 | } 24 | 25 | public DeploymentService Build() 26 | { 27 | return service; 28 | } 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter01/Section-1-10/DeploymentService.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace Section_1_10 4 | { 5 | public class DeploymentService 6 | { 7 | public int StartDelay { get; set; } = 2000; 8 | public int ErrorRetries { get; set; } = 5; 9 | public string ReportFormat { get; set; } = "pdf"; 10 | 11 | public void Start() 12 | { 13 | Console.WriteLine( 14 | $"Deployment started with:\n" + 15 | $" Start Delay: {StartDelay}\n" + 16 | $" Error Retries: {ErrorRetries}\n" + 17 | $" Report Format: {ReportFormat}"); 18 | } 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter01/Section-1-10/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace Section_1_10 4 | { 5 | class Program 6 | { 7 | static void Main() 8 | { 9 | DeploymentService service = 10 | new DeploymentBuilder() 11 | .SetStartDelay(3000) 12 | .SetErrorRetries(3) 13 | .SetReportFormat("html") 14 | .Build(); 15 | 16 | service.Start(); 17 | } 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter01/Section-1-10/Section-1-10.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | netcoreapp3.1 6 | Section_1_10 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter01/Section-1-2/DeploymentArtifacts.cs: -------------------------------------------------------------------------------- 1 | namespace Section_1_2 2 | { 3 | public class DeploymentArtifacts 4 | { 5 | public void Validate() 6 | { 7 | System.Console.WriteLine("Validating..."); 8 | } 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter01/Section-1-2/DeploymentRepository.cs: -------------------------------------------------------------------------------- 1 | namespace Section_1_2 2 | { 3 | public class DeploymentRepository 4 | { 5 | public void SaveStatus(string status) 6 | { 7 | System.Console.WriteLine("Saving status..."); 8 | } 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter01/Section-1-2/DeploymentService.cs: -------------------------------------------------------------------------------- 1 | namespace Section_1_2 2 | { 3 | public class DeploymentService : IDeploymentService 4 | { 5 | readonly DeploymentArtifacts artifacts; 6 | readonly DeploymentRepository repository; 7 | 8 | public DeploymentService( 9 | DeploymentArtifacts artifacts, 10 | DeploymentRepository repository) 11 | { 12 | this.artifacts = artifacts; 13 | this.repository = repository; 14 | } 15 | 16 | public void PerformValidation() 17 | { 18 | artifacts.Validate(); 19 | repository.SaveStatus("status"); 20 | } 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter01/Section-1-2/IDeploymentService.cs: -------------------------------------------------------------------------------- 1 | namespace Section_1_2 2 | { 3 | interface IDeploymentService 4 | { 5 | void PerformValidation(); 6 | } 7 | } -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter01/Section-1-2/Program.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.Extensions.DependencyInjection; 2 | using System; 3 | 4 | namespace Section_1_2 5 | { 6 | class Program 7 | { 8 | readonly IDeploymentService service; 9 | 10 | public Program(IDeploymentService service) 11 | { 12 | this.service = service; 13 | } 14 | 15 | static void Main() 16 | { 17 | var services = new ServiceCollection(); 18 | 19 | services.AddTransient(); 20 | services.AddTransient(); 21 | services.AddTransient(); 22 | 23 | ServiceProvider serviceProvider = services.BuildServiceProvider(); 24 | 25 | IDeploymentService deploymentService = 26 | serviceProvider.GetRequiredService(); 27 | 28 | var program = new Program(deploymentService); 29 | 30 | program.StartDeployment(); 31 | } 32 | 33 | public void StartDeployment() 34 | { 35 | service.PerformValidation(); 36 | Console.WriteLine("Validation complete - continuing..."); 37 | } 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter01/Section-1-2/Section-1-2.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | net7.0 6 | Section_1_2 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter01/Section-1-3/IValidatorFactory.cs: -------------------------------------------------------------------------------- 1 | namespace Section_1_3 2 | { 3 | public interface IValidatorFactory 4 | { 5 | ThirdPartyDeploymentService CreateDeploymentService(); 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter01/Section-1-3/Program.cs: -------------------------------------------------------------------------------- 1 | namespace Section_1_3 2 | { 3 | public class Program 4 | { 5 | readonly ThirdPartyDeploymentService service; 6 | 7 | public Program(IValidatorFactory factory) 8 | { 9 | service = factory.CreateDeploymentService(); 10 | } 11 | 12 | static void Main() 13 | { 14 | var factory = new ValidatorFactory(); 15 | var program = new Program(factory); 16 | program.PerformValidation(); 17 | } 18 | 19 | void PerformValidation() 20 | { 21 | service.Validate(); 22 | } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter01/Section-1-3/Section-1-3.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | net7.0 6 | Section_1_3 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter01/Section-1-3/ThirdPartyDeploymentService.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace Section_1_3 4 | { 5 | public class ThirdPartyDeploymentService 6 | { 7 | public void Validate() 8 | { 9 | Console.WriteLine("Validated"); 10 | } 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter01/Section-1-3/ValidatorFactory.cs: -------------------------------------------------------------------------------- 1 | namespace Section_1_3 2 | { 3 | public class ValidatorFactory : IValidatorFactory 4 | { 5 | public ThirdPartyDeploymentService CreateDeploymentService() 6 | { 7 | return new ThirdPartyDeploymentService(); 8 | } 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter01/Section-1-4/DeploymentManagementBase.cs: -------------------------------------------------------------------------------- 1 | namespace Section_1_4 2 | { 3 | public abstract class DeploymentManagementBase 4 | { 5 | IDeploymentPlugin deploymentService; 6 | 7 | protected abstract IDeploymentPlugin CreateDeploymentService(); 8 | 9 | public bool Validate() 10 | { 11 | if (deploymentService == null) 12 | deploymentService = CreateDeploymentService(); 13 | 14 | return deploymentService.Validate(); 15 | } 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter01/Section-1-4/DeploymentManager1.cs: -------------------------------------------------------------------------------- 1 | namespace Section_1_4 2 | { 3 | public class DeploymentManager1 : DeploymentManagementBase 4 | { 5 | protected override IDeploymentPlugin CreateDeploymentService() 6 | { 7 | return new DeploymentPlugin1(); 8 | } 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter01/Section-1-4/DeploymentManager2.cs: -------------------------------------------------------------------------------- 1 | namespace Section_1_4 2 | { 3 | public class DeploymentManager2 : DeploymentManagementBase 4 | { 5 | protected override IDeploymentPlugin CreateDeploymentService() 6 | { 7 | return new DeploymentPlugin2(); 8 | } 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter01/Section-1-4/DeploymentPlugin1.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace Section_1_4 4 | { 5 | public class DeploymentPlugin1 : IDeploymentPlugin 6 | { 7 | public bool Validate() 8 | { 9 | Console.WriteLine("Validated Plugin 1"); 10 | return true; 11 | } 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter01/Section-1-4/DeploymentPlugin2.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace Section_1_4 4 | { 5 | public class DeploymentPlugin2 : IDeploymentPlugin 6 | { 7 | public bool Validate() 8 | { 9 | Console.WriteLine("Validated Plugin 2"); 10 | return true; 11 | } 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter01/Section-1-4/IDeploymentPlugin.cs: -------------------------------------------------------------------------------- 1 | namespace Section_1_4 2 | { 3 | public interface IDeploymentPlugin 4 | { 5 | bool Validate(); 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter01/Section-1-4/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace Section_1_4 4 | { 5 | class Program 6 | { 7 | readonly DeploymentManagementBase[] deploymentManagers; 8 | 9 | public Program(DeploymentManagementBase[] deploymentManagers) 10 | { 11 | this.deploymentManagers = deploymentManagers; 12 | } 13 | 14 | static DeploymentManagementBase[] GetPlugins() 15 | { 16 | return new DeploymentManagementBase[] 17 | { 18 | new DeploymentManager1(), 19 | new DeploymentManager2() 20 | }; 21 | } 22 | 23 | static void Main() 24 | { 25 | DeploymentManagementBase[] deploymentManagers = GetPlugins(); 26 | 27 | var program = new Program(deploymentManagers); 28 | 29 | program.Run(); 30 | } 31 | 32 | void Run() 33 | { 34 | foreach (var manager in deploymentManagers) 35 | manager.Validate(); 36 | } 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter01/Section-1-4/Section-1-4.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | net7.0 6 | Section_1_4 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter01/Section-1-5/MultiLayer/EnterpriseApp.CmdLine/EnterpriseApp.CmdLine.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | netcoreapp3.1 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter01/Section-1-5/MultiLayer/EnterpriseApp.CmdLine/Program.cs: -------------------------------------------------------------------------------- 1 | using EnterpriseApp.CmdLine.View; 2 | 3 | namespace EnterpriseApp.CmdLine 4 | { 5 | class Program 6 | { 7 | SignIn signIn = new SignIn(); 8 | Menu menu = new Menu(); 9 | 10 | static void Main() 11 | { 12 | new Program().Start(); 13 | } 14 | 15 | void Start() 16 | { 17 | signIn.Greet(); 18 | menu.Show(); 19 | } 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter01/Section-1-5/MultiLayer/EnterpriseApp.CmdLine/View/Menu.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | 5 | namespace EnterpriseApp.CmdLine.View 6 | { 7 | public class Menu 8 | { 9 | public void Show() 10 | { 11 | Console.WriteLine( 12 | "*------*\n" + 13 | "* Menu *\n" + 14 | "*------*\n" + 15 | "\n" + 16 | "1. ...\n" + 17 | "2. ...\n" + 18 | "3. ...\n" + 19 | "\n" + 20 | "Choose: "); 21 | } 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter01/Section-1-5/MultiLayer/EnterpriseApp.CmdLine/View/SignIn.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace EnterpriseApp.CmdLine.View 4 | { 5 | public class SignIn 6 | { 7 | Greeting greeting = new Greeting(); 8 | 9 | public void Greet() 10 | { 11 | Console.Write("Is this your first visit? (true/false): "); 12 | string newResponse = Console.ReadLine(); 13 | 14 | bool.TryParse(newResponse, out bool isNew); 15 | 16 | string greetResponse = greeting.GetGreeting(isNew); 17 | 18 | Console.WriteLine($"\n*\n* {greetResponse} \n*\n"); 19 | } 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter01/Section-1-5/MultiLayer/EnterpriseApp.Data/EnterpriseApp.Data.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | netcoreapp3.1 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter01/Section-1-5/MultiLayer/EnterpriseApp.Data/GreetingRepository.cs: -------------------------------------------------------------------------------- 1 | namespace EnterpriseApp.Data 2 | { 3 | public class GreetingRepository 4 | { 5 | public string GetNewGreeting() => "Welcome!"; 6 | 7 | public string GetVisitGreeting() => "Welcome back!"; 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter01/Section-1-5/MultiLayer/EnterpriseApp/EnterpriseApp.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | netcoreapp3.1 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter01/Section-1-5/MultiLayer/EnterpriseApp/Greeting.cs: -------------------------------------------------------------------------------- 1 | using EnterpriseApp.Data; 2 | 3 | namespace EnterpriseApp 4 | { 5 | public class Greeting 6 | { 7 | GreetingRepository greetRep = new GreetingRepository(); 8 | 9 | public string GetGreeting(bool isNew) => 10 | isNew ? greetRep.GetNewGreeting() : greetRep.GetVisitGreeting(); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter01/Section-1-5/SeparateData/SystemApp.Console/Model/Greeting.cs: -------------------------------------------------------------------------------- 1 | using SystemApp.Data; 2 | 3 | namespace SystemApp.Console.Model 4 | { 5 | public class Greeting 6 | { 7 | GreetingRepository greetRep = new GreetingRepository(); 8 | 9 | public string GetGreeting(bool isNew) => 10 | isNew ? greetRep.GetNewGreeting() : greetRep.GetVisitGreeting(); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter01/Section-1-5/SeparateData/SystemApp.Console/Program.cs: -------------------------------------------------------------------------------- 1 | using SystemApp.Console.View; 2 | 3 | namespace SystemApp.Console 4 | { 5 | class Program 6 | { 7 | SignIn signIn = new SignIn(); 8 | Menu menu = new Menu(); 9 | 10 | static void Main() 11 | { 12 | new Program().Start(); 13 | } 14 | 15 | void Start() 16 | { 17 | signIn.Greet(); 18 | menu.Show(); 19 | } 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter01/Section-1-5/SeparateData/SystemApp.Console/SystemApp.Console.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | netcoreapp3.1 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter01/Section-1-5/SeparateData/SystemApp.Console/View/Menu.cs: -------------------------------------------------------------------------------- 1 | namespace SystemApp.Console.View 2 | { 3 | public class Menu 4 | { 5 | public void Show() 6 | { 7 | System.Console.WriteLine( 8 | "*------*\n" + 9 | "* Menu *\n" + 10 | "*------*\n" + 11 | "\n" + 12 | "1. ...\n" + 13 | "2. ...\n" + 14 | "3. ...\n" + 15 | "\n" + 16 | "Choose: "); 17 | } 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter01/Section-1-5/SeparateData/SystemApp.Console/View/SignIn.cs: -------------------------------------------------------------------------------- 1 | using SystemApp.Console.Model; 2 | 3 | namespace SystemApp.Console.View 4 | { 5 | public class SignIn 6 | { 7 | Greeting greeting = new Greeting(); 8 | 9 | public void Greet() 10 | { 11 | System.Console.Write("Is this your first visit? (true/false): "); 12 | string newResponse = System.Console.ReadLine(); 13 | 14 | bool.TryParse(newResponse, out bool isNew); 15 | 16 | string greetResponse = greeting.GetGreeting(isNew); 17 | 18 | System.Console.WriteLine($"\n*\n* {greetResponse} \n*\n"); 19 | } 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter01/Section-1-5/SeparateData/SystemApp.Data/GreetingRepository.cs: -------------------------------------------------------------------------------- 1 | namespace SystemApp.Data 2 | { 3 | public class GreetingRepository 4 | { 5 | public string GetNewGreeting() => "Welcome!"; 6 | 7 | public string GetVisitGreeting() => "Welcome back!"; 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter01/Section-1-5/SeparateData/SystemApp.Data/SystemApp.Data.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | netcoreapp3.1 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter01/Section-1-5/SingleProject/SimpleApp/Data/GreetingRepository.cs: -------------------------------------------------------------------------------- 1 | namespace SimpleApp.Data 2 | { 3 | public class GreetingRepository 4 | { 5 | public string GetNewGreeting() => "Welcome!"; 6 | 7 | public string GetVisitGreeting() => "Welcome back!"; 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter01/Section-1-5/SingleProject/SimpleApp/Model/Greeting.cs: -------------------------------------------------------------------------------- 1 | using SimpleApp.Data; 2 | 3 | namespace SimpleApp.Model 4 | { 5 | public class Greeting 6 | { 7 | GreetingRepository greetRep = new GreetingRepository(); 8 | 9 | public string GetGreeting(bool isNew) => 10 | isNew ? greetRep.GetNewGreeting() : greetRep.GetVisitGreeting(); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter01/Section-1-5/SingleProject/SimpleApp/Program.cs: -------------------------------------------------------------------------------- 1 | using SimpleApp.View; 2 | 3 | namespace SimpleApp 4 | { 5 | class Program 6 | { 7 | SignIn signIn = new SignIn(); 8 | Menu menu = new Menu(); 9 | 10 | static void Main() 11 | { 12 | new Program().Start(); 13 | } 14 | 15 | void Start() 16 | { 17 | signIn.Greet(); 18 | menu.Show(); 19 | } 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter01/Section-1-5/SingleProject/SimpleApp/SimpleApp.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | netcoreapp3.1 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter01/Section-1-5/SingleProject/SimpleApp/View/Menu.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace SimpleApp.View 4 | { 5 | public class Menu 6 | { 7 | public void Show() 8 | { 9 | Console.WriteLine( 10 | "*------*\n" + 11 | "* Menu *\n" + 12 | "*------*\n" + 13 | "\n" + 14 | "1. ...\n" + 15 | "2. ...\n" + 16 | "3. ...\n" + 17 | "\n" + 18 | "Choose: "); 19 | } 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter01/Section-1-5/SingleProject/SimpleApp/View/SignIn.cs: -------------------------------------------------------------------------------- 1 | using SimpleApp.Model; 2 | using System; 3 | 4 | namespace SimpleApp.View 5 | { 6 | public class SignIn 7 | { 8 | Greeting greeting = new Greeting(); 9 | 10 | public void Greet() 11 | { 12 | Console.Write("Is this your first visit? (true/false): "); 13 | string newResponse = Console.ReadLine(); 14 | 15 | bool.TryParse(newResponse, out bool isNew); 16 | 17 | string greetResponse = greeting.GetGreeting(isNew); 18 | 19 | Console.WriteLine($"\n*\n* {greetResponse} \n*\n"); 20 | } 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter01/Section-1-6/DeploymentService.cs: -------------------------------------------------------------------------------- 1 | namespace Section_1_6 2 | { 3 | public class DeploymentService 4 | { 5 | public 6 | (bool deployment, bool smokeTest, bool artifacts) 7 | PrepareDeployment() 8 | { 9 | ValidationStatus status = Validate(); 10 | 11 | (bool deployment, bool smokeTest, bool artifacts) = status; 12 | 13 | return (deployment, smokeTest, artifacts); 14 | } 15 | 16 | ValidationStatus Validate() 17 | { 18 | return new ValidationStatus 19 | { 20 | Deployment = true, 21 | SmokeTest = true, 22 | Artifacts = true 23 | }; 24 | } 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter01/Section-1-6/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace Section_1_6 4 | { 5 | class Program 6 | { 7 | readonly DeploymentService deployment = new DeploymentService(); 8 | static void Main(string[] args) 9 | { 10 | new Program().Start(); 11 | } 12 | 13 | void Start() 14 | { 15 | (bool deployed, bool smokeTest, bool artifacts) = 16 | deployment.PrepareDeployment(); 17 | 18 | Console.WriteLine( 19 | $"\nDeployment Status:\n\n" + 20 | $"Is Previous Deployment Complete? {deployed}\n" + 21 | $"Is Previous Smoke Test Complete? {smokeTest}\n" + 22 | $"Are artifacts for this deployment ready? {artifacts}\n\n" + 23 | $"Can deploy: {deployed && smokeTest && artifacts}"); 24 | } 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter01/Section-1-6/Section-1-6.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | netcoreapp3.1 6 | Section_1_6 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter01/Section-1-6/ValidationStatus.cs: -------------------------------------------------------------------------------- 1 | namespace Section_1_6 2 | { 3 | public class ValidationStatus 4 | { 5 | public bool Deployment { get; set; } 6 | public bool SmokeTest { get; set; } 7 | public bool Artifacts { get; set; } 8 | 9 | public void Deconstruct( 10 | out bool isPreviousDeploymentComplete, 11 | out bool isSmokeTestComplete, 12 | out bool areArtifactsReady) 13 | { 14 | isPreviousDeploymentComplete = Deployment; 15 | isSmokeTestComplete = SmokeTest; 16 | areArtifactsReady = Artifacts; 17 | } 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter01/Section-1-7/CircularQueueGeneric.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace Section_1_7.Generic 4 | { 5 | public class CircularQueue 6 | { 7 | int current = 0; 8 | int last = 0; 9 | T[] items; 10 | 11 | public CircularQueue(int size) 12 | { 13 | items = new T[size]; 14 | } 15 | 16 | public void Add(T obj) 17 | { 18 | if (last >= items.Length) 19 | throw new IndexOutOfRangeException(); 20 | 21 | items[last++] = obj; 22 | } 23 | 24 | public T Next() 25 | { 26 | current %= last; 27 | T item = items[current]; 28 | current++; 29 | 30 | return item; 31 | } 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter01/Section-1-7/CircularQueueObjects.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace Section_1_7.Objects 4 | { 5 | public class CircularQueue 6 | { 7 | int current = 0; 8 | int last = 0; 9 | object[] items; 10 | 11 | public CircularQueue(int size) 12 | { 13 | items = new object[size]; 14 | } 15 | 16 | public void Add(object obj) 17 | { 18 | if (last >= items.Length) 19 | throw new IndexOutOfRangeException(); 20 | 21 | items[last++] = obj; 22 | } 23 | 24 | public object Next() 25 | { 26 | current %= last; 27 | object item = items[current]; 28 | current++; 29 | 30 | return item; 31 | } 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter01/Section-1-7/Deployment.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace Section_1_7 4 | { 5 | public class Deployment 6 | { 7 | string config; 8 | 9 | public Deployment(string config) 10 | { 11 | this.config = config; 12 | } 13 | 14 | public bool PerformHealthCheck() 15 | { 16 | Console.WriteLine($"Performed health check for config {config}."); 17 | return true; 18 | } 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter01/Section-1-7/HealthChecksGeneric.cs: -------------------------------------------------------------------------------- 1 | using Section_1_7.Generic; 2 | 3 | namespace Section_1_7 4 | { 5 | public class HealthChecksGeneric 6 | { 7 | public void PerformHealthChecks(int cycles) 8 | { 9 | CircularQueue checks = Configure(); 10 | 11 | for (int i = 0; i < cycles; i++) 12 | { 13 | Deployment deployment = checks.Next(); 14 | deployment.PerformHealthCheck(); 15 | } 16 | } 17 | 18 | private CircularQueue Configure() 19 | { 20 | var queue = new CircularQueue(5); 21 | 22 | queue.Add(new Deployment("a")); 23 | queue.Add(new Deployment("b")); 24 | queue.Add(new Deployment("c")); 25 | 26 | return queue; 27 | } 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter01/Section-1-7/HealthChecksObjects.cs: -------------------------------------------------------------------------------- 1 | using Section_1_7.Objects; 2 | 3 | namespace Section_1_7 4 | { 5 | public class HealthChecksObjects 6 | { 7 | public void PerformHealthChecks(int cycles) 8 | { 9 | CircularQueue checks = Configure(); 10 | 11 | for (int i = 0; i < cycles; i++) 12 | { 13 | Deployment deployment = (Deployment)checks.Next(); 14 | deployment.PerformHealthCheck(); 15 | } 16 | } 17 | 18 | private CircularQueue Configure() 19 | { 20 | var queue = new CircularQueue(5); 21 | 22 | queue.Add(new Deployment("a")); 23 | queue.Add(new Deployment("b")); 24 | queue.Add(new Deployment("c")); 25 | 26 | return queue; 27 | } 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter01/Section-1-7/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace Section_1_7 4 | { 5 | class Program 6 | { 7 | static void Main(string[] args) 8 | { 9 | new HealthChecksObjects().PerformHealthChecks(5); 10 | new HealthChecksGeneric().PerformHealthChecks(5); 11 | } 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter01/Section-1-7/Section-1-7.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | netcoreapp3.1 6 | Section_1_7 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter01/Section-1-8/DeploymentService1.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace Section_1_8 4 | { 5 | public class DeploymentService1 : IDeploymentService 6 | { 7 | public void Validate() 8 | { 9 | Console.WriteLine("Deployment Service 1 Validated"); 10 | } 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter01/Section-1-8/DeploymentService2.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace Section_1_8 4 | { 5 | public class DeploymentService2 : IDeploymentService 6 | { 7 | public void Validate() 8 | { 9 | Console.WriteLine("Deployment Service 2 Validated"); 10 | } 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter01/Section-1-8/IDeploymentService.cs: -------------------------------------------------------------------------------- 1 | namespace Section_1_8 2 | { 3 | public interface IDeploymentService 4 | { 5 | void Validate(); 6 | } 7 | } -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter01/Section-1-8/Program.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | 3 | namespace Section_1_8 4 | { 5 | class Program 6 | { 7 | static void Main(string[] args) 8 | { 9 | new Program().Start(); 10 | } 11 | 12 | void Start() 13 | { 14 | List services = Configure(); 15 | 16 | foreach (var svc in services) 17 | svc.Validate(); 18 | } 19 | 20 | List Configure() 21 | { 22 | return new List 23 | { 24 | new DeploymentService1(), 25 | new DeploymentService2(), 26 | new ThirdPartyDeploymentAdapter() 27 | }; 28 | } 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter01/Section-1-8/Section-1-8.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | netcoreapp3.1 6 | Section_1_8 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter01/Section-1-8/ThirdPartyDeploymentAdapter.cs: -------------------------------------------------------------------------------- 1 | namespace Section_1_8 2 | { 3 | public class ThirdPartyDeploymentAdapter : IDeploymentService 4 | { 5 | ThirdPartyDeploymentService service = new ThirdPartyDeploymentService(); 6 | 7 | public void Validate() 8 | { 9 | service.PerformValidation(); 10 | } 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter01/Section-1-8/ThirdPartyDeploymentService.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | 5 | namespace Section_1_8 6 | { 7 | public class ThirdPartyDeploymentService 8 | { 9 | public void PerformValidation() 10 | { 11 | Console.WriteLine("3rd Party Deployment Service 1 Validated"); 12 | } 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter01/Section-1-9/DeploymentService.cs: -------------------------------------------------------------------------------- 1 | namespace Section_1_9 2 | { 3 | public class DeploymentService 4 | { 5 | public void Validate() 6 | { 7 | throw new DeploymentValidationException( 8 | "Smoke test failed - check with qa@example.com.", 9 | ValidationFailureReason.SmokeTestFailed); 10 | } 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter01/Section-1-9/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace Section_1_9 4 | { 5 | class Program 6 | { 7 | static void Main() 8 | { 9 | try 10 | { 11 | new DeploymentService().Validate(); 12 | } 13 | catch (DeploymentValidationException ex) 14 | { 15 | Console.WriteLine( 16 | $"Message: {ex.Message}\n" + 17 | $"Reason: {ex.Reason}\n" + 18 | $"Full Description: \n {ex}"); 19 | } 20 | } 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter01/Section-1-9/Section-1-9.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | netcoreapp3.1 6 | Section_1_9 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter01/Section-1-9/ValidationFailureReason.cs: -------------------------------------------------------------------------------- 1 | namespace Section_1_9 2 | { 3 | public enum ValidationFailureReason 4 | { 5 | Unknown, 6 | PreviousDeploymentFailed, 7 | SmokeTestFailed, 8 | MissingArtifacts 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter02/Section-02-01/Section-02-01.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | netcoreapp3.1 6 | Section_02_01 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter02/Section-02-02/Invoice.txt: -------------------------------------------------------------------------------- 1 | abc 2 | abd 3 | 4 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter02/Section-02-02/Section-02-02.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | net7.0 6 | Section_02_02 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter02/Section-02-03/Section-02-03.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | netcoreapp3.1 6 | Section_02_03 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter02/Section-02-04/BankInvoice.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace Section_02_04 4 | { 5 | public class BankInvoice : IInvoice 6 | { 7 | public void CalculateBalance() 8 | { 9 | Console.WriteLine("Calculating balance for BankInvoice."); 10 | } 11 | 12 | public bool IsApproved() 13 | { 14 | Console.WriteLine("Checking approval for BankInvoice."); 15 | return true; 16 | } 17 | 18 | public void PopulateLineItems() 19 | { 20 | Console.WriteLine("Populating items for BankInvoice."); 21 | } 22 | 23 | public void SetDueDate() 24 | { 25 | Console.WriteLine("Setting due date for BankInvoice."); 26 | } 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter02/Section-02-04/EnterpriseInvoice.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace Section_02_04 4 | { 5 | public class EnterpriseInvoice : IInvoice 6 | { 7 | public void CalculateBalance() 8 | { 9 | Console.WriteLine("Calculating balance for EnterpriseInvoice."); 10 | } 11 | 12 | public bool IsApproved() 13 | { 14 | Console.WriteLine("Checking approval for EnterpriseInvoice."); 15 | return true; 16 | } 17 | 18 | public void PopulateLineItems() 19 | { 20 | Console.WriteLine("Populating items for EnterpriseInvoice."); 21 | } 22 | 23 | public void SetDueDate() 24 | { 25 | Console.WriteLine("Setting due date for EnterpriseInvoice."); 26 | } 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter02/Section-02-04/GovernmentInvoice.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace Section_02_04 4 | { 5 | public class GovernmentInvoice : IInvoice 6 | { 7 | public void CalculateBalance() 8 | { 9 | Console.WriteLine("Calculating balance for GovernmentInvoice."); 10 | } 11 | 12 | public bool IsApproved() 13 | { 14 | Console.WriteLine("Checking approval for GovernmentInvoice."); 15 | return true; 16 | } 17 | 18 | public void PopulateLineItems() 19 | { 20 | Console.WriteLine("Populating items for GovernmentInvoice."); 21 | } 22 | 23 | public void SetDueDate() 24 | { 25 | Console.WriteLine("Setting due date for GovernmentInvoice."); 26 | } 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter02/Section-02-04/IInvoice.cs: -------------------------------------------------------------------------------- 1 | namespace Section_02_04 2 | { 3 | public interface IInvoice 4 | { 5 | bool IsApproved(); 6 | 7 | void PopulateLineItems(); 8 | 9 | void CalculateBalance(); 10 | 11 | void SetDueDate(); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter02/Section-02-04/Program.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | 3 | namespace Section_02_04 4 | { 5 | class Program 6 | { 7 | static void Main(string[] args) 8 | { 9 | IEnumerable invoices = GetInvoices(); 10 | 11 | foreach (var invoice in invoices) 12 | { 13 | if (invoice.IsApproved()) 14 | { 15 | invoice.CalculateBalance(); 16 | invoice.PopulateLineItems(); 17 | invoice.SetDueDate(); 18 | } 19 | } 20 | } 21 | 22 | static IEnumerable GetInvoices() 23 | { 24 | return new List 25 | { 26 | new BankInvoice(), 27 | new EnterpriseInvoice(), 28 | new GovernmentInvoice() 29 | }; 30 | } 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter02/Section-02-04/Section-02-04.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | netcoreapp3.1 6 | Section_02_04 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter02/Section-02-05/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | 4 | namespace Section_02_05 5 | { 6 | class Program 7 | { 8 | static void Main(string[] args) 9 | { 10 | List allInvoices = GetAllInvoices(); 11 | 12 | Console.WriteLine($"# of All Invoices: {allInvoices.Count}"); 13 | 14 | var invoicesToProcess = new List(); 15 | 16 | foreach (var invoice in allInvoices) 17 | { 18 | if (!invoicesToProcess.Contains(invoice)) 19 | invoicesToProcess.Add(invoice); 20 | } 21 | 22 | Console.WriteLine($"# of Invoices to Process: {invoicesToProcess.Count}"); 23 | } 24 | 25 | static List GetAllInvoices() 26 | { 27 | DateTime date = DateTime.Now; 28 | 29 | return new List 30 | { 31 | new Invoice { CustomerID = 1, Created = date }, 32 | new Invoice { CustomerID = 2, Created = date }, 33 | new Invoice { CustomerID = 1, Created = date }, 34 | new Invoice { CustomerID = 3, Created = date } 35 | }; 36 | } 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter02/Section-02-05/Section-02-05.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | netcoreapp3.1 6 | Section_02_05 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter02/Section-02-06/Section-02-06.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | netcoreapp3.1 6 | Section_02_06 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter02/Section-02-07/Section-02-07.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | netcoreapp3.1 6 | Section_02_07 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter02/Section-02-08/IInvoiceRepository.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | 3 | namespace Section_02_08 4 | { 5 | public interface IInvoiceRepository 6 | { 7 | List GetInvoiceCategories(); 8 | } 9 | } -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter02/Section-02-08/InvoiceCategory.cs: -------------------------------------------------------------------------------- 1 | namespace Section_02_08 2 | { 3 | public class InvoiceCategory 4 | { 5 | public int ID { get; set; } 6 | 7 | public string Name { get; set; } 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter02/Section-02-08/InvoiceRepository.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | 3 | namespace Section_02_08 4 | { 5 | public class InvoiceRepository : IInvoiceRepository 6 | { 7 | static List invoiceCategories; 8 | 9 | public List GetInvoiceCategories() 10 | { 11 | if (invoiceCategories == null) 12 | invoiceCategories = GetInvoiceCategoriesFromDB(); 13 | 14 | return invoiceCategories; 15 | } 16 | 17 | List GetInvoiceCategoriesFromDB() 18 | { 19 | return new List 20 | { 21 | new InvoiceCategory { ID = 1, Name = "Government" }, 22 | new InvoiceCategory { ID = 2, Name = "Financial" }, 23 | new InvoiceCategory { ID = 3, Name = "Enterprise" }, 24 | }; 25 | } 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter02/Section-02-08/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | 4 | namespace Section_02_08 5 | { 6 | class Program 7 | { 8 | readonly IInvoiceRepository invoiceRep; 9 | 10 | public Program(IInvoiceRepository invoiceRep) 11 | { 12 | this.invoiceRep = invoiceRep; 13 | } 14 | 15 | void Run() 16 | { 17 | List categories = 18 | invoiceRep.GetInvoiceCategories(); 19 | 20 | foreach (var category in categories) 21 | Console.WriteLine( 22 | $"ID: {category.ID}, Name: {category.Name}"); 23 | } 24 | 25 | static void Main() 26 | { 27 | new Program(new InvoiceRepository()).Run(); 28 | } 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter02/Section-02-08/Section-02-08.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | netcoreapp3.1 6 | Section_02_08 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter02/Section-02-09/IInvoiceRepository.cs: -------------------------------------------------------------------------------- 1 | namespace Section_02_09 2 | { 3 | public interface IInvoiceRepository 4 | { 5 | void AddInvoiceCategory(string category); 6 | } 7 | } -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter02/Section-02-09/InvoiceCategory.cs: -------------------------------------------------------------------------------- 1 | namespace Section_02_09 2 | { 3 | public class InvoiceCategory 4 | { 5 | public int ID { get; set; } 6 | 7 | public string Name { get; set; } 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter02/Section-02-09/InvoiceRepository.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace Section_02_09 4 | { 5 | public class InvoiceRepository : IInvoiceRepository 6 | { 7 | public InvoiceRepository() 8 | { 9 | Console.WriteLine("InvoiceRepository Instantiated."); 10 | } 11 | 12 | public void AddInvoiceCategory(string category) 13 | { 14 | Console.WriteLine($"for category: {category}"); 15 | } 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter02/Section-02-09/Section-02-09.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | netcoreapp3.1 6 | Section_02_09 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter02/Section-02-10/Section-02-10.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | netcoreapp3.1 6 | Section_02_10 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter03/Section-03-01/OrdersLibrary/CustomerType.cs: -------------------------------------------------------------------------------- 1 | namespace OrdersLibrary 2 | { 3 | public enum CustomerType 4 | { 5 | Bronze, 6 | Silver, 7 | Gold 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter03/Section-03-01/OrdersLibrary/Order.cs: -------------------------------------------------------------------------------- 1 | namespace OrdersLibrary 2 | { 3 | public class Order 4 | { 5 | public decimal CalculateDiscount( 6 | CustomerType custType, decimal amount) 7 | { 8 | decimal discount; 9 | 10 | switch (custType) 11 | { 12 | case CustomerType.Silver: 13 | discount = amount * 1.05m; 14 | break; 15 | case CustomerType.Gold: 16 | discount = amount * 1.10m; 17 | break; 18 | case CustomerType.Bronze: 19 | default: 20 | discount = amount; 21 | break; 22 | } 23 | 24 | return discount; 25 | } 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter03/Section-03-01/OrdersLibrary/OrdersLibrary.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | netcoreapp3.1 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter03/Section-03-02/CompanyOrder.cs: -------------------------------------------------------------------------------- 1 | namespace Section_03_02 2 | { 3 | public class CompanyOrder : IOrder 4 | { 5 | decimal total = 25.00m; 6 | 7 | public string PrintOrder() 8 | { 9 | return "Company Order Details"; 10 | } 11 | 12 | public decimal GetRewards() 13 | { 14 | return total * 0.01m; 15 | } 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter03/Section-03-02/CustomerOrder.cs: -------------------------------------------------------------------------------- 1 | namespace Section_03_02 2 | { 3 | class CustomerOrder : IOrder 4 | { 5 | public string PrintOrder() 6 | { 7 | return "Customer Order Details"; 8 | } 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter03/Section-03-02/IOrder.cs: -------------------------------------------------------------------------------- 1 | namespace Section_03_02 2 | { 3 | public interface IOrder 4 | { 5 | string PrintOrder(); 6 | 7 | decimal GetRewards() => 0.00m; 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter03/Section-03-02/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | 4 | namespace Section_03_02 5 | { 6 | class Program 7 | { 8 | static void Main() 9 | { 10 | var orders = new List 11 | { 12 | new CustomerOrder(), 13 | new CompanyOrder() 14 | }; 15 | 16 | foreach (var order in orders) 17 | { 18 | Console.WriteLine(order.PrintOrder()); 19 | Console.WriteLine($"Rewards: {order.GetRewards()}"); 20 | } 21 | } 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter03/Section-03-02/Section-03-02.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | netcoreapp3.1 6 | Section_03_02 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter03/Section-03-03/Section-03-03.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | netcoreapp3.1 6 | Section_03_03 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter03/Section-03-04/OrderLibraryNonNull.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | 4 | namespace Section_03_04 5 | { 6 | public class OrderLibraryNonNull 7 | { 8 | // nullable property 9 | public string DealOfTheDay { get; set; } 10 | 11 | // method with null parameter 12 | public void AddItem(string item) 13 | { 14 | Console.Write(item.ToString()); 15 | } 16 | 17 | // method with null return value 18 | public List GetItems() 19 | { 20 | return null; 21 | } 22 | 23 | // method with null type parameter 24 | public void AddItems(List items) 25 | { 26 | foreach (var item in items) 27 | Console.WriteLine(item.ToString()); 28 | } 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter03/Section-03-04/OrderLibraryWithNull.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | 4 | namespace Section_03_04 5 | { 6 | public class OrderLibraryWithNull 7 | { 8 | // nullable property 9 | public string? DealOfTheDay { get; set; } 10 | 11 | // method with null parameter 12 | public void AddItem(string? item) 13 | { 14 | _ = item ?? throw new ArgumentNullException( 15 | nameof(item), $"{nameof(item)} must not be null"); 16 | 17 | Console.Write(item.ToString()); 18 | } 19 | 20 | // method with null return value 21 | public List? GetItems() 22 | { 23 | return null; 24 | } 25 | 26 | // method with null type parameter 27 | public void AddItems(List items) 28 | { 29 | foreach (var item in items) 30 | Console.WriteLine(item?.ToString() ?? "None"); 31 | } 32 | } 33 | } -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter03/Section-03-04/Section-03-04.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | netcoreapp3.1 6 | Section_03_04 7 | enable 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter03/Section-03-05/Delivery.cs: -------------------------------------------------------------------------------- 1 | namespace Section_03_05 2 | { 3 | public class Delivery 4 | { 5 | public const string NextDay = "Next Day"; 6 | public const string Standard = "Standard"; 7 | public const string LowFare = "Low Fare"; 8 | 9 | public const int StandardDays = 7; 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter03/Section-03-05/Order.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | 3 | namespace Section_03_05 4 | { 5 | public class Order 6 | { 7 | public string DeliveryInstructions { get; set; } 8 | 9 | public List Items { get; set; } 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter03/Section-03-05/Section-03-05.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | netcoreapp3.1 6 | Section_03_05 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter03/Section-03-06/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace Section_03_06 4 | { 5 | class Program 6 | { 7 | static void Main(string[] args) 8 | { 9 | var order = new Order 10 | { 11 | ID = 7, 12 | CustomerName = "Acme", 13 | Created = DateTime.Now, 14 | Amount = 2_718_281.83m 15 | }; 16 | 17 | Console.WriteLine(order); 18 | } 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter03/Section-03-06/Section-03-06.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | net7.0 6 | Section_03_06 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter03/Section-03-07/OrderOrchestrator.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace Section_03_07 4 | { 5 | public class OrderOrchestrator 6 | { 7 | public static void HandleOrdersWrong() 8 | { 9 | try 10 | { 11 | new Orders().Process(); 12 | } 13 | catch (IndexOutOfRangeException ex) 14 | { 15 | throw new InvalidOperationException(ex.Message); 16 | } 17 | } 18 | 19 | public static void HandleOrdersBetter1() 20 | { 21 | try 22 | { 23 | new Orders().Process(); 24 | } 25 | catch (IndexOutOfRangeException ex) 26 | { 27 | throw new InvalidOperationException("Error Processing Orders", ex); 28 | } 29 | } 30 | 31 | public static void HandleOrdersBetter2() 32 | { 33 | try 34 | { 35 | new Orders().Process(); 36 | } 37 | catch (IndexOutOfRangeException) 38 | { 39 | throw; 40 | } 41 | } 42 | 43 | public static void DontHandleOrders() 44 | { 45 | new Orders().Process(); 46 | } 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter03/Section-03-07/Orders.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace Section_03_07 4 | { 5 | public class Orders 6 | { 7 | public void Process() 8 | { 9 | throw new IndexOutOfRangeException( 10 | "Expected 10 orders, but found only 9."); 11 | } 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter03/Section-03-07/Section-03-07.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | netcoreapp3.1 6 | Section_03_07 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter03/Section-03-08/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace Section_03_08 4 | { 5 | class Program 6 | { 7 | static void Main() 8 | { 9 | // Don't do this: 10 | //Console.WriteLine("Processing Orders Started"); 11 | 12 | //ProcessOrders(); 13 | 14 | //Console.WriteLine("Processing Orders Complete"); 15 | 16 | try 17 | { 18 | Console.WriteLine("Processing Orders Started"); 19 | 20 | ProcessOrders(); 21 | } 22 | catch (ArgumentException ae) 23 | { 24 | Console.WriteLine('\n' + ae.ToString() + '\n'); 25 | } 26 | finally 27 | { 28 | Console.WriteLine("Processing Orders Complete"); 29 | } 30 | } 31 | 32 | static void ProcessOrders() 33 | { 34 | throw new ArgumentException(); 35 | } 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter03/Section-03-08/Section-03-08.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | net7.0 6 | Section_03_08 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter03/Section-03-09/Section-03-09.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | net7.0 6 | Section_03_09 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter03/Section-03-10/Section-03-10.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | netcoreapp3.1 6 | Section_03_10 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter04/Section-04-01/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Linq; 3 | 4 | namespace Section_04_01 5 | { 6 | class Program 7 | { 8 | static void Main() 9 | { 10 | var context = new InMemoryContext(); 11 | 12 | var salesPersonLookup = 13 | (from person in context.SalesPeople 14 | select (person.ID, person.Name)) 15 | .ToList(); 16 | 17 | Console.WriteLine("Sales People\n"); 18 | 19 | salesPersonLookup.ForEach(person => 20 | Console.WriteLine($"{person.ID}. {person.Name}")); 21 | } 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter04/Section-04-01/SalesPerson.cs: -------------------------------------------------------------------------------- 1 | namespace Section_04_01 2 | { 3 | public class SalesPerson 4 | { 5 | public int ID { get; set; } 6 | 7 | public string Name { get; set; } 8 | 9 | public string Address { get; set; } 10 | 11 | public string City { get; set; } 12 | 13 | public string PostalCode { get; set; } 14 | 15 | public string Region { get; set; } 16 | 17 | public string ProductType { get; set; } 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter04/Section-04-01/Section-04-01.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | net7.0 6 | Section_04_01 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter04/Section-04-02/Product.cs: -------------------------------------------------------------------------------- 1 | namespace Section_04_02 2 | { 3 | public class Product 4 | { 5 | public int ID { get; set; } 6 | 7 | public string Name { get; set; } 8 | 9 | public string Type { get; set; } 10 | 11 | public decimal Price { get; set; } 12 | 13 | public string Region { get; set; } 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter04/Section-04-02/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Linq; 3 | 4 | namespace Section_04_02 5 | { 6 | class Program 7 | { 8 | static void Main() 9 | { 10 | var context = new InMemoryContext(); 11 | 12 | var salesProducts = 13 | (from person in context.SalesPeople 14 | join product in context.Products on 15 | (person.Region, person.ProductType) 16 | equals 17 | (product.Region, product.Type) 18 | select new 19 | { 20 | Person = person.Name, 21 | Product = product.Name, 22 | product.Region, 23 | product.Type 24 | }) 25 | .ToList(); 26 | 27 | Console.WriteLine("Sales People\n"); 28 | 29 | salesProducts.ForEach(salesProd => 30 | Console.WriteLine( 31 | $"Person: {salesProd.Person}\n" + 32 | $"Product: {salesProd.Product}\n" + 33 | $"Region: {salesProd.Region}\n" + 34 | $"Type: {salesProd.Type}\n")); 35 | } 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter04/Section-04-02/SalesPerson.cs: -------------------------------------------------------------------------------- 1 | namespace Section_04_02 2 | { 3 | public class SalesPerson 4 | { 5 | public int ID { get; set; } 6 | 7 | public string Name { get; set; } 8 | 9 | public string Address { get; set; } 10 | 11 | public string City { get; set; } 12 | 13 | public string PostalCode { get; set; } 14 | 15 | public string Region { get; set; } 16 | 17 | public string ProductType { get; set; } 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter04/Section-04-02/Section-04-02.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | net7.0 6 | Section_04_02 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter04/Section-04-03/Product.cs: -------------------------------------------------------------------------------- 1 | namespace Section_04_03 2 | { 3 | public class Product 4 | { 5 | public int ID { get; set; } 6 | 7 | public string Name { get; set; } 8 | 9 | public string Type { get; set; } 10 | 11 | public decimal Price { get; set; } 12 | 13 | public string Region { get; set; } 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter04/Section-04-03/SalesPerson.cs: -------------------------------------------------------------------------------- 1 | namespace Section_04_03 2 | { 3 | public class SalesPerson 4 | { 5 | public int ID { get; set; } 6 | 7 | public string Name { get; set; } 8 | 9 | public string Address { get; set; } 10 | 11 | public string City { get; set; } 12 | 13 | public string PostalCode { get; set; } 14 | 15 | public string Region { get; set; } 16 | 17 | public string ProductType { get; set; } 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter04/Section-04-03/Section-04-03.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | Exe 5 | net7.0 6 | Section_04_03 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter04/Section-04-04/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Linq; 3 | 4 | namespace Section_04_04 5 | { 6 | class Program 7 | { 8 | static void Main() 9 | { 10 | var context = new InMemoryContext(); 11 | 12 | var salesPeopleByRegion = 13 | (from person in context.SalesPeople 14 | group person by person.Region 15 | into personGroup 16 | select personGroup) 17 | .ToList(); 18 | 19 | Console.WriteLine("Sales People by Region"); 20 | 21 | foreach (var region in salesPeopleByRegion) 22 | { 23 | Console.WriteLine($"\nRegion: {region.Key}"); 24 | 25 | foreach (var person in region) 26 | Console.WriteLine($" {person.Name}"); 27 | } 28 | } 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter04/Section-04-04/SalesPerson.cs: -------------------------------------------------------------------------------- 1 | namespace Section_04_04 2 | { 3 | public class SalesPerson 4 | { 5 | public int ID { get; set; } 6 | 7 | public string Name { get; set; } 8 | 9 | public string Address { get; set; } 10 | 11 | public string City { get; set; } 12 | 13 | public string PostalCode { get; set; } 14 | 15 | public string Region { get; set; } 16 | 17 | public string ProductType { get; set; } 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter04/Section-04-04/Section-04-04.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | net7.0 6 | Section_04_04 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter04/Section-04-05/SalesPerson.cs: -------------------------------------------------------------------------------- 1 | namespace Section_04_05 2 | { 3 | public class SalesPerson 4 | { 5 | public int ID { get; set; } 6 | 7 | public string Name { get; set; } 8 | 9 | public string Address { get; set; } 10 | 11 | public string City { get; set; } 12 | 13 | public string PostalCode { get; set; } 14 | 15 | public string Region { get; set; } 16 | 17 | public string ProductType { get; set; } 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter04/Section-04-05/Section-04-05.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | net7.0 6 | Section_04_05 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter04/Section-04-06/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | 5 | namespace Section_04_06 6 | { 7 | class Program 8 | { 9 | static void Main(string[] args) 10 | { 11 | var salesPeopleWithoutComparer = 12 | (from person in new InMemoryContext().SalesPeople 13 | select person) 14 | .Distinct() 15 | .ToList(); 16 | 17 | PrintResults(salesPeopleWithoutComparer, "Without Comparer"); 18 | 19 | var salesPeopleWithComparer = 20 | (from person in new InMemoryContext().SalesPeople 21 | select person) 22 | .Distinct(new SalesPersonComparer()) 23 | .ToList(); 24 | 25 | PrintResults(salesPeopleWithComparer, "With Comparer"); 26 | } 27 | 28 | static void PrintResults(List salesPeople, string title) 29 | { 30 | Console.WriteLine($"\n{title}\n"); 31 | 32 | salesPeople.ForEach(person => 33 | Console.WriteLine($"{person.ID}. {person.Name}")); 34 | } 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter04/Section-04-06/SalesPerson.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | 3 | namespace Section_04_06 4 | { 5 | public class SalesPersonComparer : IEqualityComparer 6 | { 7 | public bool Equals(SalesPerson x, SalesPerson y) 8 | { 9 | return x.ID == y.ID; 10 | } 11 | 12 | public int GetHashCode(SalesPerson obj) 13 | { 14 | return obj.GetHashCode(); 15 | } 16 | } 17 | 18 | public class SalesPerson 19 | { 20 | public int ID { get; set; } 21 | 22 | public string Name { get; set; } 23 | 24 | public string Address { get; set; } 25 | 26 | public string City { get; set; } 27 | 28 | public string PostalCode { get; set; } 29 | 30 | public string Region { get; set; } 31 | 32 | public string ProductType { get; set; } 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter04/Section-04-06/Section-04-06.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | net7.0 6 | Section_04_06 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter04/Section-04-07/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Linq; 3 | 4 | namespace Section_04_07 5 | { 6 | class Program 7 | { 8 | static void Main(string[] args) 9 | { 10 | decimal TotalSales = 0; 11 | 12 | var salesPeopleWithAddresses = 13 | (from person in new InMemoryContext().SalesPeople 14 | let FullAddress = 15 | $"{person.Address}\n" + 16 | $"{person.City}, {person.PostalCode}" 17 | let salesOkay = 18 | decimal.TryParse(person.TotalSales, out TotalSales) 19 | select new 20 | { 21 | person.ID, 22 | person.Name, 23 | FullAddress, 24 | TotalSales 25 | }) 26 | .ToList(); 27 | 28 | Console.WriteLine($"\nSales People and Addresses\n"); 29 | 30 | salesPeopleWithAddresses.ForEach(person => 31 | Console.WriteLine( 32 | $"{person.ID}. {person.Name}: {person.TotalSales:C}\n" + 33 | $"{person.FullAddress}\n")); 34 | } 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter04/Section-04-07/SalesPerson.cs: -------------------------------------------------------------------------------- 1 | namespace Section_04_07 2 | { 3 | public class SalesPerson 4 | { 5 | public int ID { get; set; } 6 | 7 | public string Name { get; set; } 8 | 9 | public string Address { get; set; } 10 | 11 | public string City { get; set; } 12 | 13 | public string PostalCode { get; set; } 14 | 15 | public string Region { get; set; } 16 | 17 | public string ProductType { get; set; } 18 | 19 | public string TotalSales { get; set; } 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter04/Section-04-07/Section-04-07.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | net7.0 6 | Section_04_07 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter04/Section-04-08/SalesPerson.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | 3 | namespace Section_04_08 4 | { 5 | public class SalesPerson : IEqualityComparer 6 | { 7 | public int ID { get; set; } 8 | 9 | public string Name { get; set; } 10 | 11 | public string Address { get; set; } 12 | 13 | public string City { get; set; } 14 | 15 | public string PostalCode { get; set; } 16 | 17 | public string Region { get; set; } 18 | 19 | public string ProductType { get; set; } 20 | 21 | public bool Equals(SalesPerson x, SalesPerson y) 22 | { 23 | return x.ID == y.ID; 24 | } 25 | 26 | public int GetHashCode(SalesPerson obj) 27 | { 28 | return ID.GetHashCode(); 29 | } 30 | } 31 | } -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter04/Section-04-08/Section-04-08.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | net7.0 6 | Section_04_08 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter04/Section-04-09/SalesPerson.cs: -------------------------------------------------------------------------------- 1 | namespace Section_04_09 2 | { 3 | public class SalesPerson 4 | { 5 | public int ID { get; set; } 6 | 7 | public string Name { get; set; } 8 | 9 | public string Address { get; set; } 10 | 11 | public string City { get; set; } 12 | 13 | public string PostalCode { get; set; } 14 | 15 | public string Region { get; set; } 16 | 17 | public string ProductType { get; set; } 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter04/Section-04-09/Section-04-09.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | net7.0 6 | Section_04_09 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter04/Section-04-10/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Threading; 5 | 6 | namespace Section_04_10 7 | { 8 | class Program 9 | { 10 | static void Main() 11 | { 12 | List salesPeople = new InMemoryContext().SalesPeople; 13 | var result = 14 | (from person in salesPeople.AsParallel() 15 | select ProcessPerson(person)) 16 | .ToList(); 17 | } 18 | 19 | static SalesPerson ProcessPerson(SalesPerson person) 20 | { 21 | Console.WriteLine( 22 | $"Starting sales person " + 23 | $"#{person.ID}. {person.Name}"); 24 | 25 | // complex in-memory processing 26 | Thread.Sleep(500); 27 | 28 | Console.WriteLine( 29 | $"Completed sales person " + 30 | $"#{person.ID}. {person.Name}"); 31 | 32 | return person; 33 | } 34 | } 35 | } -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter04/Section-04-10/SalesPerson.cs: -------------------------------------------------------------------------------- 1 | namespace Section_04_10 2 | { 3 | public class SalesPerson 4 | { 5 | public int ID { get; set; } 6 | 7 | public string Name { get; set; } 8 | 9 | public string Address { get; set; } 10 | 11 | public string City { get; set; } 12 | 13 | public string PostalCode { get; set; } 14 | 15 | public string Region { get; set; } 16 | 17 | public string ProductType { get; set; } 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter04/Section-04-10/Section-04-10.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | net7.0 6 | Section_04_10 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter05/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JoeMayo/csharp-nine-cookbook/a22b77eb3e472a847035297dac9d4594a96f458f/CSharp9Cookbook/Chapter05/.DS_Store -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter05/Section-05-01/ColumnAttribute.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace Section_05_01 4 | { 5 | [AttributeUsage( 6 | AttributeTargets.Property | AttributeTargets.Method, 7 | AllowMultiple = false)] 8 | public class ColumnAttribute : Attribute 9 | { 10 | public ColumnAttribute(string name) 11 | { 12 | Name = name; 13 | } 14 | 15 | public string Name { get; set; } 16 | 17 | public string Format { get; set; } 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter05/Section-05-01/InventoryItem.cs: -------------------------------------------------------------------------------- 1 | namespace Section_05_01 2 | { 3 | public class InventoryItem 4 | { 5 | [Column("Part #")] 6 | public string PartNumber { get; set; } 7 | 8 | [Column("Name")] 9 | public string Description { get; set; } 10 | 11 | [Column("Amount")] 12 | public int Count { get; set; } 13 | 14 | [Column("Price")] 15 | public decimal ItemPrice { get; set; } 16 | 17 | [Column("Total")] 18 | public decimal CalculateTotal() 19 | { 20 | return ItemPrice * Count; 21 | } 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter05/Section-05-01/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | 4 | namespace Section_05_01 5 | { 6 | class Program 7 | { 8 | static void Main() 9 | { 10 | var inventory = new List 11 | { 12 | new InventoryItem 13 | { 14 | PartNumber = "1", 15 | Description = "Part #1", 16 | Count = 3, 17 | ItemPrice = 5.26m 18 | }, 19 | new InventoryItem 20 | { 21 | PartNumber = "2", 22 | Description = "Part #2", 23 | Count = 1, 24 | ItemPrice = 7.95m 25 | }, 26 | new InventoryItem 27 | { 28 | PartNumber = "3", 29 | Description = "Part #3", 30 | Count = 2, 31 | ItemPrice = 23.13m 32 | }, 33 | }; 34 | 35 | string report = new Report().Generate(inventory); 36 | 37 | Console.WriteLine(report); 38 | } 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter05/Section-05-01/Section-05-01.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | net7.0 6 | Section_05_01 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter05/Section-05-02/ColumnAttribute.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace Section_05_02 4 | { 5 | [AttributeUsage( 6 | AttributeTargets.Property | AttributeTargets.Method, 7 | AllowMultiple = false)] 8 | public class ColumnAttribute : Attribute 9 | { 10 | public ColumnAttribute(string name) 11 | { 12 | Name = name; 13 | } 14 | 15 | public string Name { get; set; } 16 | 17 | public string Format { get; set; } 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter05/Section-05-02/ColumnDetail.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | 3 | namespace Section_05_02 4 | { 5 | public class ColumnDetail 6 | { 7 | public string Name { get; set; } 8 | 9 | public ColumnAttribute Attribute { get; set; } 10 | 11 | public PropertyInfo PropertyInfo { get; set; } 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter05/Section-05-02/InventoryItem.cs: -------------------------------------------------------------------------------- 1 | namespace Section_05_02 2 | { 3 | public class InventoryItem 4 | { 5 | [Column("Part #")] 6 | public string PartNumber { get; set; } 7 | 8 | [Column("Name")] 9 | public string Description { get; set; } 10 | 11 | [Column("Amount")] 12 | public int Count { get; set; } 13 | 14 | [Column("Price", Format = "{0:c}")] 15 | public decimal ItemPrice { get; set; } 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter05/Section-05-02/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | 4 | namespace Section_05_02 5 | { 6 | class Program 7 | { 8 | static void Main() 9 | { 10 | var inventory = new List 11 | { 12 | new InventoryItem 13 | { 14 | PartNumber = "1", 15 | Description = "Part #1", 16 | Count = 3, 17 | ItemPrice = 5.26m 18 | }, 19 | new InventoryItem 20 | { 21 | PartNumber = "2", 22 | Description = "Part #2", 23 | Count = 1, 24 | ItemPrice = 7.95m 25 | }, 26 | new InventoryItem 27 | { 28 | PartNumber = "3", 29 | Description = "Part #3", 30 | Count = 2, 31 | ItemPrice = 23.13m 32 | }, 33 | }; 34 | 35 | string report = new Report().Generate(inventory); 36 | 37 | Console.WriteLine(report); 38 | } 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter05/Section-05-02/Section-05-02.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | net7.0 6 | Section_05_02 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter05/Section-05-03/ColumnAttribute.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace Section_05_03 4 | { 5 | [AttributeUsage( 6 | AttributeTargets.Property | AttributeTargets.Method, 7 | AllowMultiple = false)] 8 | public class ColumnAttribute : Attribute 9 | { 10 | public ColumnAttribute(string name) 11 | { 12 | Name = name; 13 | } 14 | 15 | public string Name { get; set; } 16 | 17 | public string Format { get; set; } 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter05/Section-05-03/ColumnDetail.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | 3 | namespace Section_05_03 4 | { 5 | public class ColumnDetail 6 | { 7 | public string Name { get; set; } 8 | 9 | public ColumnAttribute Attribute { get; set; } 10 | 11 | public PropertyInfo PropertyInfo { get; set; } 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter05/Section-05-03/InventoryItem.cs: -------------------------------------------------------------------------------- 1 | namespace Section_05_03 2 | { 3 | public class InventoryItem 4 | { 5 | [Column("Part #")] 6 | public string PartNumber { get; set; } 7 | 8 | [Column("Name")] 9 | public string Description { get; set; } 10 | 11 | [Column("Amount")] 12 | public int Count { get; set; } 13 | 14 | [Column("Price", Format = "{0:c}")] 15 | public decimal ItemPrice { get; set; } 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter05/Section-05-03/ReportType.cs: -------------------------------------------------------------------------------- 1 | namespace Section_05_03 2 | { 3 | public enum ReportType 4 | { 5 | Html, 6 | Markdown 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter05/Section-05-03/Section-05-03.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | net7.0 6 | Section_05_03 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter05/Section-05-04/ColumnAttribute.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace Section_05_04 4 | { 5 | [AttributeUsage( 6 | AttributeTargets.Property | AttributeTargets.Method, 7 | AllowMultiple = false)] 8 | public class ColumnAttribute : Attribute 9 | { 10 | public ColumnAttribute(string name) 11 | { 12 | Name = name; 13 | } 14 | 15 | public string Name { get; set; } 16 | 17 | public string Format { get; set; } 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter05/Section-05-04/ColumnDetail.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | 3 | namespace Section_05_04 4 | { 5 | public class ColumnDetail 6 | { 7 | public string Name { get; set; } 8 | 9 | public ColumnAttribute Attribute { get; set; } 10 | 11 | public MemberInfo MemberInfo { get; set; } 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter05/Section-05-04/InventoryItem.cs: -------------------------------------------------------------------------------- 1 | namespace Section_05_04 2 | { 3 | public class InventoryItem 4 | { 5 | [Column("Part #")] 6 | public string PartNumber { get; set; } 7 | 8 | [Column("Name")] 9 | public string Description { get; set; } 10 | 11 | [Column("Amount")] 12 | public int Count { get; set; } 13 | 14 | [Column("Price", Format = "{0:c}")] 15 | public decimal ItemPrice { get; set; } 16 | 17 | [Column("Total", Format = "{0:c}")] 18 | public decimal CalculateTotal() 19 | { 20 | return ItemPrice * Count; 21 | } 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter05/Section-05-04/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | 4 | namespace Section_05_04 5 | { 6 | class Program 7 | { 8 | static void Main() 9 | { 10 | var inventory = new List 11 | { 12 | new InventoryItem 13 | { 14 | PartNumber = "1", 15 | Description = "Part #1", 16 | Count = 3, 17 | ItemPrice = 5.26m 18 | }, 19 | new InventoryItem 20 | { 21 | PartNumber = "2", 22 | Description = "Part #2", 23 | Count = 1, 24 | ItemPrice = 7.95m 25 | }, 26 | new InventoryItem 27 | { 28 | PartNumber = "3", 29 | Description = "Part #3", 30 | Count = 2, 31 | ItemPrice = 23.13m 32 | }, 33 | }; 34 | 35 | string report = new Report().Generate(inventory); 36 | 37 | Console.WriteLine(report); 38 | } 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter05/Section-05-04/Section-05-04.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | net7.0 6 | Section_05_04 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter05/Section-05-05/ColumnAttribute.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace Section_05_05 4 | { 5 | [AttributeUsage( 6 | AttributeTargets.Property | AttributeTargets.Method, 7 | AllowMultiple = false)] 8 | public class ColumnAttribute : Attribute 9 | { 10 | public ColumnAttribute(string name) 11 | { 12 | Name = name; 13 | } 14 | 15 | public string Name { get; set; } 16 | 17 | public string Format { get; set; } 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter05/Section-05-05/ColumnDetail.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | 3 | namespace Section_05_05 4 | { 5 | public class ColumnDetail 6 | { 7 | public string Name { get; set; } 8 | 9 | public ColumnAttribute Attribute { get; set; } 10 | 11 | public PropertyInfo PropertyInfo { get; set; } 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter05/Section-05-05/Inventory.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | 3 | namespace Section_05_05 4 | { 5 | public class Inventory 6 | { 7 | public string Title { get; set; } 8 | 9 | public List Data { get; set; } 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter05/Section-05-05/InventoryItem.cs: -------------------------------------------------------------------------------- 1 | namespace Section_05_05 2 | { 3 | public class InventoryItem 4 | { 5 | [Column("Part #")] 6 | public string PartNumber { get; set; } 7 | 8 | [Column("Name")] 9 | public string Description { get; set; } 10 | 11 | [Column("Amount")] 12 | public int Count { get; set; } 13 | 14 | [Column("Price", Format = "{0:c}")] 15 | public decimal ItemPrice { get; set; } 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter05/Section-05-05/Section-05-05.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | net7.0 6 | Section_05_05 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter05/Section-05-06/ColumnAttribute.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace Section_05_06 4 | { 5 | [AttributeUsage( 6 | AttributeTargets.Property | AttributeTargets.Method, 7 | AllowMultiple = false)] 8 | public class ColumnAttribute : Attribute 9 | { 10 | public ColumnAttribute(string name) 11 | { 12 | Name = name; 13 | } 14 | 15 | public string Name { get; set; } 16 | 17 | public string Format { get; set; } 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter05/Section-05-06/ColumnDetail.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | 3 | namespace Section_05_06 4 | { 5 | public class ColumnDetail 6 | { 7 | public string Name { get; set; } 8 | 9 | public ColumnAttribute Attribute { get; set; } 10 | 11 | public PropertyInfo PropertyInfo { get; set; } 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter05/Section-05-06/InventoryItem.cs: -------------------------------------------------------------------------------- 1 | namespace Section_05_06 2 | { 3 | public class InventoryItem 4 | { 5 | [Column("Part #")] 6 | public string PartNumber { get; set; } 7 | 8 | [Column("Name")] 9 | public string Description { get; set; } 10 | 11 | [Column("Amount")] 12 | public int Count { get; set; } 13 | 14 | [Column("Price", Format = "{0:c}")] 15 | public decimal ItemPrice { get; set; } 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter05/Section-05-06/Report.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | 4 | namespace Section_05_06 5 | { 6 | public class Report 7 | { 8 | public string Generate(List items, ReportType reportType) 9 | { 10 | GeneratorBase generator = CreateGenerator(reportType); 11 | 12 | string report = generator.Generate(items); 13 | 14 | return report; 15 | } 16 | 17 | GeneratorBase CreateGenerator(ReportType reportType) 18 | { 19 | Type dataType = typeof(TData); 20 | 21 | string generatorNamespace = "Section_05_06."; 22 | string generatorTypeName = $"{reportType}Generator`1"; 23 | string typeParameterName = $"[[{dataType.FullName}]]"; 24 | 25 | string fullyQualifiedTypeName = 26 | generatorNamespace + 27 | generatorTypeName + 28 | typeParameterName; 29 | 30 | Type generatorType = Type.GetType(fullyQualifiedTypeName); 31 | 32 | object generator = Activator.CreateInstance(generatorType); 33 | 34 | return (GeneratorBase)generator; 35 | } 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter05/Section-05-06/ReportType.cs: -------------------------------------------------------------------------------- 1 | namespace Section_05_06 2 | { 3 | public enum ReportType 4 | { 5 | Html, 6 | Markdown, 7 | ExcelTyped, 8 | ExcelDynamic 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter05/Section-05-06/Section-05-06.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | net7.0 6 | Section_05_06 7 | 8 | 9 | 10 | 11 | ..\..\..\..\..\..\Program Files (x86)\Microsoft Visual Studio\Shared\Visual Studio Tools for Office\PIA\Office15\Microsoft.Office.Interop.Excel.dll 12 | 13 | 14 | 15 | 16 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter05/Section-05-07/ColumnAttribute.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace Section_05_07 4 | { 5 | [AttributeUsage( 6 | AttributeTargets.Property | AttributeTargets.Method, 7 | AllowMultiple = false)] 8 | public class ColumnAttribute : Attribute 9 | { 10 | public ColumnAttribute(string name) 11 | { 12 | Name = name; 13 | } 14 | 15 | public string Name { get; set; } 16 | 17 | public string Format { get; set; } 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter05/Section-05-07/ColumnDetail.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | 3 | namespace Section_05_07 4 | { 5 | public class ColumnDetail 6 | { 7 | public string Name { get; set; } 8 | 9 | public ColumnAttribute Attribute { get; set; } 10 | 11 | public PropertyInfo PropertyInfo { get; set; } 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter05/Section-05-07/LogEntry.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | namespace Section_05_07 3 | { 4 | public class LogEntry 5 | { 6 | [Column("Log Date", Format = "{0:yyyy-MM-dd hh:mm}")] 7 | public DateTime CreatedAt { get; set; } 8 | 9 | [Column("Severity")] 10 | public string Type { get; set; } 11 | 12 | [Column("Location")] 13 | public string Where { get; set; } 14 | 15 | [Column("Message")] 16 | public string Description { get; set; } 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter05/Section-05-07/Section-05-07.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | net7.0 6 | Section_05_07 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter05/Section-05-08/ColumnAttribute.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace Section_05_08 4 | { 5 | [AttributeUsage( 6 | AttributeTargets.Property | AttributeTargets.Method, 7 | AllowMultiple = false)] 8 | public class ColumnAttribute : Attribute 9 | { 10 | public ColumnAttribute(string name) 11 | { 12 | Name = name; 13 | } 14 | 15 | public string Name { get; set; } 16 | 17 | public string Format { get; set; } 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter05/Section-05-08/ColumnDetail.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | 3 | namespace Section_05_08 4 | { 5 | public class ColumnDetail 6 | { 7 | public string Name { get; set; } 8 | 9 | public ColumnAttribute Attribute { get; set; } 10 | 11 | public PropertyInfo PropertyInfo { get; set; } 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter05/Section-05-08/LogEntry.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | namespace Section_05_08 3 | { 4 | public class LogEntry 5 | { 6 | [Column("Log Date", Format = "{0:yyyy-MM-dd hh:mm}")] 7 | public DateTime CreatedAt { get; set; } 8 | 9 | [Column("Severity")] 10 | public string Type { get; set; } 11 | 12 | [Column("Location")] 13 | public string Where { get; set; } 14 | 15 | [Column("Message")] 16 | public string Description { get; set; } 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter05/Section-05-08/Section-05-08.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | net7.0 6 | Section_05_08 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter05/Section-05-09/ColumnAttribute.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace Section_05_09 4 | { 5 | [AttributeUsage( 6 | AttributeTargets.Property | AttributeTargets.Method, 7 | AllowMultiple = false)] 8 | public class ColumnAttribute : Attribute 9 | { 10 | public ColumnAttribute(string name) 11 | { 12 | Name = name; 13 | } 14 | 15 | public string Name { get; set; } 16 | 17 | public string Format { get; set; } 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter05/Section-05-09/ColumnDetail.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | 3 | namespace Section_05_09 4 | { 5 | public class ColumnDetail 6 | { 7 | public string Name { get; set; } 8 | 9 | public ColumnAttribute Attribute { get; set; } 10 | 11 | public PropertyInfo PropertyInfo { get; set; } 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter05/Section-05-09/Section-05-09.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | net7.0 6 | Section_05_09 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter05/Section-05-09/Semantic.py: -------------------------------------------------------------------------------- 1 | import sys 2 | #sys.path.append( 3 | # "/System/Library/Frameworks/Python.framework" + 4 | # "/Versions/Current/lib/python2.7") 5 | 6 | from random import * 7 | 8 | class SemanticAnalysis: 9 | @staticmethod 10 | def Eval(text): 11 | val = random() 12 | return val < .5 -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter05/Section-05-09/Tweet.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace Section_05_09 4 | { 5 | public class Tweet 6 | { 7 | [Column("Screen Name")] 8 | public string ScreenName { get; set; } 9 | 10 | [Column("Date")] 11 | public DateTime CreatedAt { get; set; } 12 | 13 | [Column("Text")] 14 | public string Text { get; set; } 15 | 16 | [Column("Semantic Analysis")] 17 | public string Semantics { get; set; } 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter05/Section-05-10-old/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JoeMayo/csharp-nine-cookbook/a22b77eb3e472a847035297dac9d4594a96f458f/CSharp9Cookbook/Chapter05/Section-05-10-old/.DS_Store -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter05/Section-05-10-old/ColumnAttribute.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace Section_05_10 4 | { 5 | [AttributeUsage( 6 | AttributeTargets.Property | AttributeTargets.Method, 7 | AllowMultiple = false)] 8 | public class ColumnAttribute : Attribute 9 | { 10 | public ColumnAttribute(string name) 11 | { 12 | Name = name; 13 | } 14 | 15 | public string Name { get; set; } 16 | 17 | public string Format { get; set; } 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter05/Section-05-10-old/ColumnDetail.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | 3 | namespace Section_05_10 4 | { 5 | public class ColumnDetail 6 | { 7 | public string Name { get; set; } 8 | 9 | public ColumnAttribute Attribute { get; set; } 10 | 11 | public PropertyInfo PropertyInfo { get; set; } 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter05/Section-05-10-old/InventoryItem.cs: -------------------------------------------------------------------------------- 1 | namespace Section_05_10 2 | { 3 | public class InventoryItem 4 | { 5 | [Column("Part #")] 6 | public string PartNumber { get; set; } 7 | 8 | [Column("Name")] 9 | public string Description { get; set; } 10 | 11 | [Column("Amount")] 12 | public int Count { get; set; } 13 | 14 | [Column("Price", Format = "{0:c}")] 15 | public decimal ItemPrice { get; set; } 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter05/Section-05-10-old/MainApp.py: -------------------------------------------------------------------------------- 1 | import clr, os, sys 2 | 3 | sys.path.append(r"C:\Projects\csharp-nine-cookbook\csharp-nine-cookbook\CSharp9Cookbook\Chapter05\Section-05-10\bin\Debug\netcoreapp3.0") 4 | clr.AddReference(r"C:\Projects\csharp-nine-cookbook\csharp-nine-cookbook\CSharp9Cookbook\Chapter05\Section-05-10\bin\Debug\netcoreapp3.0\Section-05-10.dll") 5 | 6 | from Section_05_10 import Report 7 | 8 | class InventoryItem: 9 | def __init__(self, PartNumber, Description, Count, ItemPrice): 10 | self.PartNumber = PartNumber 11 | self.Description = Description 12 | self.Count = Count 13 | 14 | 15 | inventory = [] 16 | inventory.append(InventoryItem("1", "Part #1", 3, 5.26)) 17 | inventory.append(InventoryItem("2", "Part #2", 1, 7.95)) 18 | inventory.append(InventoryItem("3", "Part #1", 2, 23.13)) 19 | 20 | rpt = Report() 21 | 22 | result = rpt.GenerateDynamic(inventory) 23 | 24 | print(result) -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter05/Section-05-10-old/Section-05-10.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | netcoreapp3.0 5 | Section_05_10 6 | Section-05-10 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter05/Section-05-10/ColumnAttribute.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace PythonToCS 4 | { 5 | [AttributeUsage( 6 | AttributeTargets.Property | AttributeTargets.Method, 7 | AllowMultiple = false)] 8 | public class ColumnAttribute : Attribute 9 | { 10 | public ColumnAttribute(string name) 11 | { 12 | Name = name; 13 | } 14 | 15 | public string Name { get; set; } 16 | 17 | public string Format { get; set; } 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter05/Section-05-10/ColumnDetail.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | 3 | namespace PythonToCS 4 | { 5 | public class ColumnDetail 6 | { 7 | public string Name { get; set; } 8 | 9 | public ColumnAttribute Attribute { get; set; } 10 | 11 | public PropertyInfo PropertyInfo { get; set; } 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter05/Section-05-10/InventoryItem.cs: -------------------------------------------------------------------------------- 1 | namespace PythonToCS 2 | { 3 | public class InventoryItem 4 | { 5 | public InventoryItem( 6 | string partNumber, string description, 7 | int count, decimal itemPrice) 8 | { 9 | PartNumber = partNumber; 10 | Description = description; 11 | Count = count; 12 | ItemPrice = itemPrice; 13 | } 14 | 15 | [Column("Part #")] 16 | public string PartNumber { get; set; } 17 | 18 | [Column("Name")] 19 | public string Description { get; set; } 20 | 21 | [Column("Amount")] 22 | public int Count { get; set; } 23 | 24 | [Column("Price", Format = "{0:c}")] 25 | public decimal ItemPrice { get; set; } 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter05/Section-05-10/MainApp.py: -------------------------------------------------------------------------------- 1 | import clr, sys 2 | 3 | sys.path.append(r"C:\Projects\csharp-nine-cookbook\csharp-nine-cookbook\CSharp9Cookbook\Chapter05\Section-05-10\bin\Debug") 4 | clr.AddReference(r"C:\Projects\csharp-nine-cookbook\csharp-nine-cookbook\CSharp9Cookbook\Chapter05\Section-05-10\bin\Debug\PythonToCS.dll") 5 | 6 | from PythonToCS import Report 7 | from PythonToCS import InventoryItem 8 | from System import Decimal 9 | 10 | inventory = [ 11 | InventoryItem("1", "Part #1", 3, Decimal(5.26)), 12 | InventoryItem("2", "Part #2", 1, Decimal(7.95)), 13 | InventoryItem("3", "Part #1", 2, Decimal(23.13))] 14 | 15 | rpt = Report() 16 | 17 | result = rpt.GenerateDynamic(inventory) 18 | 19 | print(result) -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter06/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JoeMayo/csharp-nine-cookbook/a22b77eb3e472a847035297dac9d4594a96f458f/CSharp9Cookbook/Chapter06/.DS_Store -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter06/Section-06-01/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JoeMayo/csharp-nine-cookbook/a22b77eb3e472a847035297dac9d4594a96f458f/CSharp9Cookbook/Chapter06/Section-06-01/.DS_Store -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter06/Section-06-01/CheckoutService.cs: -------------------------------------------------------------------------------- 1 | using System.Threading.Tasks; 2 | 3 | namespace Section_06_01 4 | { 5 | public class CheckoutService 6 | { 7 | public async Task StartAsync() 8 | { 9 | await ValidateAddressAsync(); 10 | await ValidateCreditAsync(); 11 | await GetShoppingCartAsync(); 12 | await FinalizeCheckoutAsync(); 13 | 14 | return "Checkout Complete"; 15 | } 16 | 17 | async Task ValidateAddressAsync() 18 | { 19 | // perform address validation 20 | } 21 | 22 | async Task ValidateCreditAsync() 23 | { 24 | // ensure credit is good 25 | } 26 | 27 | async Task GetShoppingCartAsync() 28 | { 29 | // get contents of shopping cart 30 | } 31 | 32 | async Task FinalizeCheckoutAsync() 33 | { 34 | // complete checkout transaction 35 | } 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter06/Section-06-01/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Threading.Tasks; 3 | 4 | namespace Section_06_01 5 | { 6 | class Program 7 | { 8 | //static void Main(string[] args) 9 | //{ 10 | // var checkoutSvc = new CheckoutService(); 11 | // string result = string.Empty; 12 | 13 | // Task startedTask = checkoutSvc.StartAsync(); 14 | // startedTask.Wait(); 15 | // result = startedTask.Result; 16 | 17 | // Console.WriteLine($"Result: {result}"); 18 | //} 19 | 20 | static async Task Main() 21 | { 22 | var checkoutSvc = new CheckoutService(); 23 | 24 | string result = await checkoutSvc.StartAsync(); 25 | 26 | Console.WriteLine($"Result: {result}"); 27 | } 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter06/Section-06-01/Section-06-01.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | net7.0 6 | Section_06_01 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter06/Section-06-02/CheckoutService.cs: -------------------------------------------------------------------------------- 1 | using System.Threading.Tasks; 2 | 3 | namespace Section_06_02 4 | { 5 | public class CheckoutService 6 | { 7 | public async ValueTask StartAsync() 8 | { 9 | await ValidateAddressAsync(); 10 | await ValidateCreditAsync(); 11 | await GetShoppingCartAsync(); 12 | await FinalizeCheckoutAsync(); 13 | 14 | return "Checkout Complete"; 15 | } 16 | 17 | async ValueTask ValidateAddressAsync() 18 | { 19 | // perform address validation 20 | } 21 | 22 | async ValueTask ValidateCreditAsync() 23 | { 24 | // ensure credit is good 25 | } 26 | 27 | async ValueTask GetShoppingCartAsync() 28 | { 29 | // get contents of shopping cart 30 | } 31 | 32 | async ValueTask FinalizeCheckoutAsync() 33 | { 34 | // complete checkout transaction 35 | } 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter06/Section-06-02/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Threading.Tasks; 3 | 4 | namespace Section_06_02 5 | { 6 | class Program 7 | { 8 | static async Task Main() 9 | { 10 | var checkoutSvc = new CheckoutService(); 11 | 12 | string result = await checkoutSvc.StartAsync(); 13 | 14 | Console.WriteLine($"Result: {result}"); 15 | } 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter06/Section-06-02/Section-06-02.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | net7.0 6 | Section_06_02 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter06/Section-06-03/CheckoutRequest.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | namespace Section_06_03 3 | { 4 | public class CheckoutRequest 5 | { 6 | public Guid ShoppingCartID { get; set; } 7 | 8 | public string Name { get; set; } 9 | 10 | public string Card { get; set; } 11 | 12 | public string Address { get; set; } 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter06/Section-06-03/CheckoutService.cs: -------------------------------------------------------------------------------- 1 | using System.Threading.Tasks; 2 | 3 | namespace Section_06_03 4 | { 5 | public class CheckoutService 6 | { 7 | public async ValueTask StartAsync(CheckoutRequest request) 8 | { 9 | return 10 | $"Checkout Complete for Shopping " + 11 | $"Basket: {request.ShoppingCartID}"; 12 | } 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter06/Section-06-03/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Threading.Tasks; 3 | 4 | namespace Section_06_03 5 | { 6 | class Program 7 | { 8 | static async Task Main() 9 | { 10 | var checkoutSvc = new CheckoutService(); 11 | var checkoutStrm = new CheckoutStream(); 12 | 13 | await foreach (var request in checkoutStrm.GetRequestsAsync()) 14 | { 15 | string result = await checkoutSvc.StartAsync(request); 16 | 17 | Console.WriteLine($"Result: {result}"); 18 | } 19 | } 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter06/Section-06-03/Section-06-03.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | net7.0 6 | Section_06_03 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter06/Section-06-04/CheckoutService.cs: -------------------------------------------------------------------------------- 1 | using System.Threading.Tasks; 2 | 3 | namespace Section_06_04 4 | { 5 | public class CheckoutService 6 | { 7 | public async Task StartAsync() 8 | { 9 | await ValidateAddressAsync().ConfigureAwait(false); 10 | await ValidateCreditAsync().ConfigureAwait(false); 11 | await GetShoppingCartAsync().ConfigureAwait(false); 12 | await FinalizeCheckoutAsync().ConfigureAwait(false); 13 | 14 | return "Checkout Complete"; 15 | } 16 | 17 | async Task ValidateAddressAsync() 18 | { 19 | // perform address validation 20 | } 21 | 22 | async Task ValidateCreditAsync() 23 | { 24 | // ensure credit is good 25 | } 26 | 27 | async Task GetShoppingCartAsync() 28 | { 29 | // get contents of shopping cart 30 | } 31 | 32 | async Task FinalizeCheckoutAsync() 33 | { 34 | // complete checkout transaction 35 | } 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter06/Section-06-04/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Threading.Tasks; 3 | 4 | namespace Section_06_04 5 | { 6 | class Program 7 | { 8 | static async Task Main() 9 | { 10 | var checkoutSvc = new CheckoutService(); 11 | 12 | string result = await checkoutSvc.StartAsync(); 13 | 14 | Console.WriteLine($"Result: {result}"); 15 | } 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter06/Section-06-04/Section-06-04.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | net7.0 6 | Section_06_04 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter06/Section-06-05/CheckoutRequest.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | namespace Section_06_05 3 | { 4 | public class CheckoutRequest 5 | { 6 | public Guid ShoppingCartID { get; set; } 7 | 8 | public string Name { get; set; } 9 | 10 | public string Card { get; set; } 11 | 12 | public string Address { get; set; } 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter06/Section-06-05/CheckoutRequestProgress.cs: -------------------------------------------------------------------------------- 1 | namespace Section_06_05 2 | { 3 | public class CheckoutRequestProgress 4 | { 5 | public int Total { get; set; } 6 | 7 | public string Message { get; set; } 8 | } 9 | } -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter06/Section-06-05/CheckoutService.cs: -------------------------------------------------------------------------------- 1 | using System.Threading.Tasks; 2 | 3 | namespace Section_06_05 4 | { 5 | public class CheckoutService 6 | { 7 | public async ValueTask StartAsync(CheckoutRequest request) 8 | { 9 | return 10 | $"Checkout Complete for Shopping " + 11 | $"Basket: {request.ShoppingCartID}"; 12 | } 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter06/Section-06-05/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Threading.Tasks; 3 | 4 | namespace Section_06_05 5 | { 6 | class Program 7 | { 8 | static async Task Main() 9 | { 10 | var checkoutSvc = new CheckoutService(); 11 | var checkoutStrm = new CheckoutStream(); 12 | 13 | IProgress progress = 14 | new Progress(p => 15 | { 16 | Console.WriteLine( 17 | $"\n" + 18 | $"Total: {p.Total}, " + 19 | $"{p.Message}" + 20 | $"\n"); 21 | }); 22 | 23 | await foreach (var request in checkoutStrm.GetRequestsAsync(progress)) 24 | { 25 | string result = await checkoutSvc.StartAsync(request); 26 | 27 | Console.WriteLine($"Result: {result}"); 28 | } 29 | } 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter06/Section-06-05/Section-06-05.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | net7.0 6 | Section_06_05 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter06/Section-06-06/CheckoutService.cs: -------------------------------------------------------------------------------- 1 | using System.Threading.Tasks; 2 | 3 | namespace Section_06_06 4 | { 5 | public class CheckoutService 6 | { 7 | public async Task StartAsync() 8 | { 9 | await ValidateAddressAsync().ConfigureAwait(false); 10 | await ValidateCreditAsync().ConfigureAwait(false); 11 | await GetShoppingCartAsync().ConfigureAwait(false); 12 | await FinalizeCheckoutAsync().ConfigureAwait(false); 13 | 14 | return "Checkout Complete"; 15 | } 16 | 17 | async Task ValidateAddressAsync() 18 | { 19 | bool result = true; 20 | return await Task.FromResult(result); 21 | } 22 | 23 | async Task ValidateCreditAsync() 24 | { 25 | bool result = true; 26 | return await Task.FromResult(result); 27 | } 28 | 29 | async Task GetShoppingCartAsync() 30 | { 31 | bool result = true; 32 | return await Task.FromResult(result); 33 | } 34 | 35 | async Task FinalizeCheckoutAsync() 36 | { 37 | await Task.CompletedTask; 38 | } 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter06/Section-06-06/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Threading.Tasks; 3 | 4 | namespace Section_06_06 5 | { 6 | class Program 7 | { 8 | static async Task Main() 9 | { 10 | var checkoutSvc = new CheckoutService(); 11 | 12 | string result = await checkoutSvc.StartAsync(); 13 | 14 | Console.WriteLine($"Result: {result}"); 15 | } 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter06/Section-06-06/Section-06-06.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | net7.0 6 | Section_06_06 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter06/Section-06-07/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Threading.Tasks; 3 | 4 | namespace Section_06_07 5 | { 6 | class Program 7 | { 8 | static async Task Main() 9 | { 10 | try 11 | { 12 | var checkoutSvc = new CheckoutService(); 13 | 14 | string result = await checkoutSvc.StartAsync(); 15 | 16 | Console.WriteLine($"Result: {result}"); 17 | } 18 | catch (AggregateException aEx) 19 | { 20 | foreach (var ex in aEx.InnerExceptions) 21 | Console.WriteLine($"Unable to complete: {ex}"); 22 | } 23 | } 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter06/Section-06-07/Section-06-07.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | net7.0 6 | Section_06_07 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter06/Section-06-08/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Threading.Tasks; 3 | 4 | namespace Section_06_08 5 | { 6 | class Program 7 | { 8 | static async Task Main() 9 | { 10 | try 11 | { 12 | var checkoutSvc = new CheckoutService(); 13 | 14 | string result = await checkoutSvc.StartBigO1Async(); 15 | //string result = await checkoutSvc.StartBigONAsync(); 16 | //string result = await checkoutSvc.StartBigONSquaredAsync(); 17 | 18 | Console.WriteLine($"Result: {result}"); 19 | } 20 | catch (AggregateException aEx) 21 | { 22 | foreach (var ex in aEx.InnerExceptions) 23 | Console.WriteLine($"Unable to complete: {ex}"); 24 | } 25 | } 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter06/Section-06-08/Section-06-08.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | net7.0 6 | Section_06_08 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter06/Section-06-09/CheckoutRequest.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace Section_06_09 4 | { 5 | public class CheckoutRequest 6 | { 7 | public Guid ShoppingCartID { get; set; } 8 | 9 | public string Name { get; set; } 10 | 11 | public string Card { get; set; } 12 | 13 | public string Address { get; set; } 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter06/Section-06-09/CheckoutRequestProgress.cs: -------------------------------------------------------------------------------- 1 | namespace Section_06_09 2 | { 3 | public class CheckoutRequestProgress 4 | { 5 | public int Total { get; set; } 6 | 7 | public string Message { get; set; } 8 | } 9 | } -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter06/Section-06-09/CheckoutService.cs: -------------------------------------------------------------------------------- 1 | using System.Threading.Tasks; 2 | 3 | namespace Section_06_09 4 | { 5 | public class CheckoutService 6 | { 7 | public async ValueTask StartAsync(CheckoutRequest request) 8 | { 9 | return await Task.FromResult( 10 | $"Checkout Complete for Shopping " + 11 | $"Basket: {request.ShoppingCartID}"); 12 | } 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter06/Section-06-09/Section-06-09.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | net7.0 6 | Section_06_09 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter06/Section-06-10/CheckoutRequest.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace Section_06_10 4 | { 5 | public class CheckoutRequest 6 | { 7 | public Guid ShoppingCartID { get; set; } 8 | 9 | public string Name { get; set; } 10 | 11 | public string Card { get; set; } 12 | 13 | public string Address { get; set; } 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter06/Section-06-10/CheckoutRequestProgress.cs: -------------------------------------------------------------------------------- 1 | namespace Section_06_10 2 | { 3 | public class CheckoutRequestProgress 4 | { 5 | public int Total { get; set; } 6 | 7 | public string Message { get; set; } 8 | } 9 | } -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter06/Section-06-10/CheckoutService.cs: -------------------------------------------------------------------------------- 1 | using System.Threading.Tasks; 2 | 3 | namespace Section_06_10 4 | { 5 | public class CheckoutService 6 | { 7 | public async ValueTask StartAsync(CheckoutRequest request) 8 | { 9 | return await Task.FromResult( 10 | $"Checkout Complete for Shopping " + 11 | $"Basket: {request.ShoppingCartID}"); 12 | } 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter06/Section-06-10/ConsoleLogger.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Threading.Tasks; 3 | 4 | namespace Section_06_10 5 | { 6 | public class ConsoleLogger : ILogger 7 | { 8 | public async Task WriteAsync(string message) 9 | { 10 | Console.WriteLine($"Log: {message}"); 11 | return await Task.FromResult(message); 12 | } 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter06/Section-06-10/ILogger.cs: -------------------------------------------------------------------------------- 1 | using System.Threading.Tasks; 2 | 3 | namespace Section_06_10 4 | { 5 | public interface ILogger 6 | { 7 | Task WriteAsync(string message); 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter06/Section-06-10/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Threading; 3 | using System.Threading.Tasks; 4 | 5 | namespace Section_06_10 6 | { 7 | class Program 8 | { 9 | static async Task Main() 10 | { 11 | await using var checkoutStrm = new CheckoutStream(); 12 | 13 | var checkoutSvc = new CheckoutService(); 14 | 15 | IProgress progress = 16 | new Progress(p => 17 | { 18 | Console.WriteLine( 19 | $"\n" + 20 | $"Total: {p.Total}, " + 21 | $"{p.Message}" + 22 | $"\n"); 23 | }); 24 | 25 | int count = 1; 26 | 27 | await foreach (var request in checkoutStrm.GetRequestsAsync(progress)) 28 | { 29 | string result = await checkoutSvc.StartAsync(request); 30 | 31 | Console.WriteLine($"Result: {result}"); 32 | 33 | if (count++ >= 10) 34 | break; 35 | } 36 | } 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter06/Section-06-10/Section-06-10.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | net7.0 6 | Section_06_10 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter07/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JoeMayo/csharp-nine-cookbook/a22b77eb3e472a847035297dac9d4594a96f458f/CSharp9Cookbook/Chapter07/.DS_Store -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter07/Section-07-01/Section-07-01.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | net7.0 6 | Section_07_01 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter07/Section-07-02/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Security.Cryptography; 3 | 4 | namespace Section_07_02 5 | { 6 | class Program 7 | { 8 | static void Main() 9 | { 10 | var crypto = new Crypto(); 11 | 12 | Console.Write("Please enter text to encrypt: "); 13 | string userPlainText = Console.ReadLine(); 14 | 15 | byte[] key = GenerateKey(); 16 | 17 | byte[] cypherBytes = crypto.Encrypt(userPlainText, key); 18 | 19 | string cypherText = Convert.ToBase64String(cypherBytes); 20 | 21 | Console.WriteLine($"Cypher Text: {cypherText}"); 22 | 23 | string decryptedPlainText = crypto.Decrypt(cypherBytes, key); 24 | 25 | Console.WriteLine($"Plain Text: {decryptedPlainText}"); 26 | } 27 | 28 | static byte[] GenerateKey() 29 | { 30 | const int KeyLength = 32; 31 | 32 | byte[] key = new byte[KeyLength]; 33 | var rngRand = new RNGCryptoServiceProvider(); 34 | 35 | rngRand.GetBytes(key); 36 | 37 | return key; 38 | } 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter07/Section-07-02/Section-07-02.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | net7.0 6 | Section_07_02 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter07/Section-07-03/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using Microsoft.Extensions.Configuration; 3 | 4 | namespace Section_07_03 5 | { 6 | class Program 7 | { 8 | static void Main() 9 | { 10 | var config = new ConfigurationBuilder() 11 | .AddUserSecrets() 12 | .Build(); 13 | 14 | string key = "CSharpCookbook:ApiKey"; 15 | Console.WriteLine($"{key}: {config[key]}"); 16 | } 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter07/Section-07-03/Section-07-03.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | net7.0 6 | Section_07_03 7 | d3d91a8b-d440-414a-821e-7f11eec48f32 8 | 9 | 10 | 11 | 12 | 13 | 14 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter07/Section-07-04/Program.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | 3 | namespace Section_07_04 4 | { 5 | class Program 6 | { 7 | static void Main() 8 | { 9 | PurchaseOrder po = GetPurchaseOrder(); 10 | new PurchaseOrderService().View(po); 11 | } 12 | 13 | static PurchaseOrder GetPurchaseOrder() 14 | { 15 | return new PurchaseOrder 16 | { 17 | CompanyName = "Acme, Inc.", 18 | Address = "123 4th St.", 19 | Phone = "555-835-7609", 20 | AdditionalInfo = new Dictionary 21 | { 22 | { "terms", "Net 30" }, 23 | { "poc", "J. Smith" } 24 | }, 25 | Items = new List 26 | { 27 | new PurchaseItem 28 | { 29 | Description = "Widget", 30 | Price = 13.95m, 31 | Quantity = 5, 32 | SerialNumber = "123" 33 | } 34 | } 35 | }; 36 | } 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter07/Section-07-04/PurchaseItem.cs: -------------------------------------------------------------------------------- 1 | using System.Text.Json.Serialization; 2 | 3 | namespace Section_07_04 4 | { 5 | public class PurchaseItem 6 | { 7 | [JsonPropertyName("serialNo")] 8 | public string SerialNumber { get; set; } 9 | 10 | [JsonPropertyName("description")] 11 | public string Description { get; set; } 12 | 13 | [JsonPropertyName("qty")] 14 | public float Quantity { get; set; } 15 | 16 | [JsonPropertyName("amount")] 17 | public decimal Price { get; set; } 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter07/Section-07-04/PurchaseOrder.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using System.Text.Json.Serialization; 3 | 4 | namespace Section_07_04 5 | { 6 | public class PurchaseOrder 7 | { 8 | [JsonPropertyName("company")] 9 | public string CompanyName { get; set; } 10 | [JsonPropertyName("address")] 11 | public string Address { get; set; } 12 | [JsonPropertyName("phone")] 13 | public string Phone { get; set; } 14 | 15 | [JsonPropertyName("status")] 16 | public PurchaseOrderStatus Status { get; set; } 17 | 18 | [JsonPropertyName("other")] 19 | public Dictionary AdditionalInfo { get; set; } 20 | 21 | [JsonPropertyName("details")] 22 | public List Items { get; set; } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter07/Section-07-04/PurchaseOrderService.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Text.Json; 3 | 4 | namespace Section_07_04 5 | { 6 | public class PurchaseOrderService 7 | { 8 | public void View(PurchaseOrder po) 9 | { 10 | var jsonOptions = new JsonSerializerOptions 11 | { 12 | WriteIndented = true 13 | }; 14 | 15 | string poJson = JsonSerializer.Serialize(po, jsonOptions); 16 | 17 | // send HTTP request 18 | 19 | Console.WriteLine(poJson); 20 | } 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter07/Section-07-04/PurchaseOrderStatus.cs: -------------------------------------------------------------------------------- 1 | namespace Section_07_04 2 | { 3 | public enum PurchaseOrderStatus 4 | { 5 | Received, 6 | Processing, 7 | Fulfilled 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter07/Section-07-04/Section-07-04.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | net7.0 6 | Section_07_04 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter07/Section-07-05/PurchaseItem.cs: -------------------------------------------------------------------------------- 1 | namespace Section_07_05 2 | { 3 | public class PurchaseItem 4 | { 5 | public string SerialNumber { get; set; } 6 | 7 | public string Description { get; set; } 8 | 9 | public float Quantity { get; set; } 10 | 11 | public decimal Price { get; set; } 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter07/Section-07-05/PurchaseOrder.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using System.Text.Json.Serialization; 3 | 4 | namespace Section_07_05 5 | { 6 | public class PurchaseOrder 7 | { 8 | public string CompanyName { get; set; } 9 | public string Address { get; set; } 10 | public string Phone { get; set; } 11 | 12 | [JsonConverter(typeof(PurchaseOrderStatusConverter))] 13 | public PurchaseOrderStatus Status { get; set; } 14 | 15 | public Dictionary AdditionalInfo { get; set; } 16 | 17 | public List Items { get; set; } 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter07/Section-07-05/PurchaseOrderService.cs: -------------------------------------------------------------------------------- 1 | namespace Section_07_05 2 | { 3 | public class PurchaseOrderService 4 | { 5 | public string Get(int poID) 6 | { 7 | // get HTTP request 8 | 9 | return @"{ 10 | ""company_name"": ""Acme, Inc."", 11 | ""address"": ""123 4th St."", 12 | ""phone"": ""555-835-7609"", 13 | ""additional_info"": { 14 | ""terms"": ""Net 30"", 15 | ""poc"": ""J. Smith"", 16 | }, 17 | ""status"": ""Processing"", 18 | ""items"": [ 19 | { 20 | ""serial_number"": ""123"", 21 | ""description"": ""Widget"", 22 | ""quantity"": 5, 23 | ""price"": 13.95 24 | } 25 | ] 26 | }"; 27 | } 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter07/Section-07-05/PurchaseOrderStatus.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | namespace Section_07_05 3 | { 4 | public enum PurchaseOrderStatus 5 | { 6 | Received, 7 | Processing, 8 | Fulfilled 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter07/Section-07-05/PurchaseOrderStatusConverter.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Text.Json; 3 | using System.Text.Json.Serialization; 4 | 5 | namespace Section_07_05 6 | { 7 | public class PurchaseOrderStatusConverter 8 | : JsonConverter 9 | { 10 | public override PurchaseOrderStatus Read( 11 | ref Utf8JsonReader reader, 12 | Type typeToConvert, 13 | JsonSerializerOptions options) 14 | { 15 | string statusString = reader.GetString(); 16 | 17 | if (Enum.TryParse( 18 | statusString, 19 | out PurchaseOrderStatus status)) 20 | { 21 | return status; 22 | } 23 | else 24 | { 25 | throw new JsonException( 26 | $"{statusString} is not a valid " + 27 | $"{nameof(PurchaseOrderStatus)} value."); 28 | } 29 | } 30 | 31 | public override void Write( 32 | Utf8JsonWriter writer, 33 | PurchaseOrderStatus value, 34 | JsonSerializerOptions options) 35 | { 36 | writer.WriteStringValue(value.ToString()); 37 | } 38 | } 39 | } -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter07/Section-07-05/Section-07-05.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | net7.0 6 | Section_07_05 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter07/Section-07-05/SnakeCaseNamingPolicy.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text.Json; 4 | 5 | namespace Section_07_05 6 | { 7 | public class SnakeCaseNamingPolicy : JsonNamingPolicy 8 | { 9 | public override string ConvertName(string name) 10 | { 11 | var targetChars = new List(); 12 | char[] sourceChars = name.ToCharArray(); 13 | 14 | char first = sourceChars[0]; 15 | if (char.IsUpper(first)) 16 | targetChars.Add(char.ToLower(first)); 17 | else 18 | targetChars.Add(first); 19 | 20 | for (int i = 1; i < sourceChars.Length; i++) 21 | { 22 | char ch = sourceChars[i]; 23 | 24 | if (char.IsUpper(ch)) 25 | { 26 | targetChars.Add('_'); 27 | targetChars.Add(char.ToLower(ch)); 28 | } 29 | else 30 | { 31 | targetChars.Add(ch); 32 | } 33 | } 34 | 35 | return new string(targetChars.ToArray()); 36 | } 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter07/Section-07-06/PurchaseItem.cs: -------------------------------------------------------------------------------- 1 | namespace Section_07_06 2 | { 3 | public class PurchaseItem 4 | { 5 | public string SerialNumber { get; set; } 6 | 7 | public string Description { get; set; } 8 | 9 | public double Quantity { get; set; } 10 | 11 | public decimal Price { get; set; } 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter07/Section-07-06/PurchaseOrder.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | 3 | namespace Section_07_06 4 | { 5 | public class PurchaseOrder 6 | { 7 | public string CompanyName { get; set; } 8 | public string Address { get; set; } 9 | public string Phone { get; set; } 10 | public string Terms { get; set; } 11 | public string POC { get; set; } 12 | 13 | public PurchaseOrderStatus Status { get; set; } 14 | 15 | public Dictionary AdditionalInfo { get; set; } 16 | 17 | public List Items { get; set; } 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter07/Section-07-06/PurchaseOrderService.cs: -------------------------------------------------------------------------------- 1 | namespace Section_07_06 2 | { 3 | public class PurchaseOrderService 4 | { 5 | public string Get(int poID) 6 | { 7 | // get HTTP request 8 | 9 | return @"{ 10 | ""company_name"": ""Acme, Inc."", 11 | ""address"": ""123 4th St."", 12 | ""phone"": ""555-835-7609"", 13 | ""additional_info"": { 14 | ""terms"": ""Net 30"", 15 | ""poc"": ""J. Smith"" 16 | }, 17 | ""status"": ""Processing"", 18 | ""items"": [ 19 | { 20 | ""serial_number"": ""123"", 21 | ""description"": ""Widget"", 22 | ""quantity"": 5, 23 | ""price"": 13.95 24 | } 25 | ] 26 | }"; 27 | } 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter07/Section-07-06/PurchaseOrderStatus.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | namespace Section_07_06 3 | { 4 | public enum PurchaseOrderStatus 5 | { 6 | Received, 7 | Processing, 8 | Fulfilled 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter07/Section-07-06/Section-07-06.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | net7.0 6 | Section_07_06 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter07/Section-07-07/PurchaseItem.cs: -------------------------------------------------------------------------------- 1 | namespace Section_07_07 2 | { 3 | public class PurchaseItem 4 | { 5 | public string SerialNumber { get; set; } 6 | 7 | public string Description { get; set; } 8 | 9 | public float Quantity { get; set; } 10 | 11 | public decimal Price { get; set; } 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter07/Section-07-07/PurchaseOrder.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | 3 | namespace Section_07_07 4 | { 5 | public class PurchaseOrder 6 | { 7 | public string CompanyName { get; set; } 8 | public string Address { get; set; } 9 | public string Phone { get; set; } 10 | 11 | public PurchaseOrderStatus Status { get; set; } 12 | 13 | public Dictionary AdditionalInfo { get; set; } 14 | 15 | public List Items { get; set; } 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter07/Section-07-07/PurchaseOrderStatus.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | namespace Section_07_07 3 | { 4 | public enum PurchaseOrderStatus 5 | { 6 | Received, 7 | Processing, 8 | Fulfilled 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter07/Section-07-07/Section-07-07.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | net7.0 6 | Section_07_07 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter07/Section-07-08/PurchaseItem.cs: -------------------------------------------------------------------------------- 1 | namespace Section_07_08 2 | { 3 | public class PurchaseItem 4 | { 5 | public string SerialNumber { get; set; } 6 | 7 | public string Description { get; set; } 8 | 9 | public float Quantity { get; set; } 10 | 11 | public decimal Price { get; set; } 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter07/Section-07-08/PurchaseOrder.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | 3 | namespace Section_07_08 4 | { 5 | public class PurchaseOrder 6 | { 7 | public string CompanyName { get; set; } 8 | public string Address { get; set; } 9 | public string Phone { get; set; } 10 | 11 | public PurchaseOrderStatus Status { get; set; } 12 | 13 | public Dictionary AdditionalInfo { get; set; } 14 | 15 | public List Items { get; set; } 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter07/Section-07-08/PurchaseOrderStatus.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | namespace Section_07_08 3 | { 4 | public enum PurchaseOrderStatus 5 | { 6 | Received, 7 | Processing, 8 | Fulfilled 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter07/Section-07-08/Section-07-08.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | net7.0 6 | Section_07_08 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter07/Section-07-09/Section-07-09.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | Exe 5 | net7.0 6 | Section_07_09 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter07/Section-07-10/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Text.Json; 3 | 4 | namespace Section_07_10 5 | { 6 | class Program 7 | { 8 | static void Main() 9 | { 10 | const string TweetID = "1305895383260782593"; 11 | const string CreatedDate = "created_at"; 12 | 13 | string tweetJson = GetTweet(TweetID); 14 | 15 | JsonElement tweetElem = JsonDocument.Parse(tweetJson).RootElement; 16 | 17 | DateTime created = tweetElem.GetDate(CreatedDate); 18 | 19 | Console.WriteLine($"Created Date: {created}"); 20 | } 21 | 22 | static string GetTweet(string tweetID) 23 | { 24 | return @"{ 25 | ""text"": ""Thanks @github for approving sponsorship for LINQ to Twitter: https://t.co/jWeDEN07HN"", 26 | ""id"": ""1305895383260782593"", 27 | ""author_id"": ""15411837"", 28 | ""created_at"": ""2020-09-15T15:44:56.000Z"" 29 | }"; 30 | } 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter07/Section-07-10/Section-07-10.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | net7.0 6 | Section_07_10 7 | enable 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter08/Section-08-01/BronzeSchedule.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | namespace Section_08_01 3 | { 4 | public class BronzeSchedule : IRoomSchedule 5 | { 6 | public void ScheduleRoom() => 7 | Console.WriteLine("Scheduling Bronze Room"); 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter08/Section-08-01/GoldSchedule.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | namespace Section_08_01 3 | { 4 | public class GoldSchedule : IRoomSchedule 5 | { 6 | public void ScheduleRoom() => 7 | Console.WriteLine("Scheduling Gold Room"); 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter08/Section-08-01/IRoomSchedule.cs: -------------------------------------------------------------------------------- 1 | namespace Section_08_01 2 | { 3 | public interface IRoomSchedule 4 | { 5 | void ScheduleRoom(); 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter08/Section-08-01/Section-08-01.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | net7.0 6 | Section_08_01 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter08/Section-08-01/SilverSchedule.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | namespace Section_08_01 3 | { 4 | public class SilverSchedule : IRoomSchedule 5 | { 6 | public void ScheduleRoom() => 7 | Console.WriteLine("Scheduling Silver Room"); 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter08/Section-08-02/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace Section_08_02 4 | { 5 | class Program 6 | { 7 | static void Main() 8 | { 9 | try 10 | { 11 | Console.Write("Choose (1) arg1 or (2) arg2? "); 12 | string arg = Console.ReadLine(); 13 | 14 | var scheduler = new Scheduler(); 15 | 16 | if (arg == "1") 17 | scheduler.ScheduleRoom(null, "arg2"); 18 | else 19 | scheduler.ScheduleRoom("arg1", null); 20 | } 21 | catch (ArgumentNullException ex1) 22 | when (ex1.ParamName == "arg1") 23 | { 24 | Console.WriteLine("Invalid arg1"); 25 | } 26 | catch (ArgumentNullException ex2) 27 | when (ex2.ParamName == "arg2") 28 | { 29 | Console.WriteLine("Invalid arg2"); 30 | } 31 | } 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter08/Section-08-02/Scheduler.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | namespace Section_08_02 3 | { 4 | public class Scheduler 5 | { 6 | public void ScheduleRoom(string arg1, string arg2) 7 | { 8 | _ = arg1 ?? throw new ArgumentNullException(nameof(arg1)); 9 | _ = arg2 ?? throw new ArgumentNullException(nameof(arg2)); 10 | } 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter08/Section-08-02/Section-08-02.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | net7.0 6 | Section_08_02 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter08/Section-08-03/BronzeSchedule.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | namespace Section_08_03 3 | { 4 | public class BronzeSchedule : IRoomSchedule 5 | { 6 | public void ScheduleRoom() => 7 | Console.WriteLine("Scheduling Bronze Room"); 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter08/Section-08-03/GoldSchedule.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | namespace Section_08_03 3 | { 4 | public class GoldSchedule : IRoomSchedule 5 | { 6 | public void ScheduleRoom() => 7 | Console.WriteLine("Scheduling Gold Room"); 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter08/Section-08-03/IRoomSchedule.cs: -------------------------------------------------------------------------------- 1 | namespace Section_08_03 2 | { 3 | public interface IRoomSchedule 4 | { 5 | void ScheduleRoom(); 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter08/Section-08-03/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace Section_08_03 4 | { 5 | class Program 6 | { 7 | static void Main() 8 | { 9 | Console.Write( 10 | "Choose (1) Bronze, (2) Silver, or (3) Gold: "); 11 | string choice = Console.ReadLine(); 12 | 13 | Enum.TryParse(choice, out ScheduleType scheduleType); 14 | 15 | var scheduler = new Scheduler(); 16 | 17 | IRoomSchedule scheduleStatement = 18 | scheduler.CreateStatement(scheduleType); 19 | scheduleStatement.ScheduleRoom(); 20 | 21 | IRoomSchedule scheduleExpression = 22 | scheduler.CreateExpression(scheduleType); 23 | scheduleExpression.ScheduleRoom(); 24 | } 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter08/Section-08-03/ScheduleType.cs: -------------------------------------------------------------------------------- 1 | namespace Section_08_03 2 | { 3 | public enum ScheduleType 4 | { 5 | None, 6 | Bronze, 7 | Silver, 8 | Gold 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter08/Section-08-03/Scheduler.cs: -------------------------------------------------------------------------------- 1 | namespace Section_08_03 2 | { 3 | public class Scheduler 4 | { 5 | public IRoomSchedule CreateStatement( 6 | ScheduleType scheduleType) 7 | { 8 | switch (scheduleType) 9 | { 10 | case ScheduleType.Gold: 11 | return new GoldSchedule(); 12 | case ScheduleType.Silver: 13 | return new SilverSchedule(); 14 | case ScheduleType.Bronze: 15 | default: 16 | return new BronzeSchedule(); 17 | } 18 | } 19 | 20 | public IRoomSchedule CreateExpression( 21 | ScheduleType scheduleType) => 22 | scheduleType switch 23 | { 24 | ScheduleType.Gold => new GoldSchedule(), 25 | ScheduleType.Silver => new SilverSchedule(), 26 | ScheduleType.Bronze => new BronzeSchedule(), 27 | _ => new BronzeSchedule() 28 | }; 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter08/Section-08-03/Section-08-03.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | net7.0 6 | Section_08_03 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter08/Section-08-03/SilverSchedule.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | namespace Section_08_03 3 | { 4 | public class SilverSchedule : IRoomSchedule 5 | { 6 | public void ScheduleRoom() => 7 | Console.WriteLine("Scheduling Silver Room"); 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter08/Section-08-04/Room.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | namespace Section_08_04 3 | { 4 | public class Room 5 | { 6 | public int Number { get; set; } 7 | public string RoomType { get; set; } 8 | public string BedSize { get; set; } 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter08/Section-08-04/ScheduleType.cs: -------------------------------------------------------------------------------- 1 | namespace Section_08_04 2 | { 3 | public enum ScheduleType 4 | { 5 | None, 6 | Bronze, 7 | Silver, 8 | Gold 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter08/Section-08-04/Section-08-04.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | net7.0 6 | Section_08_04 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter08/Section-08-05/Room.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | namespace Section_08_05 3 | { 4 | public class Room 5 | { 6 | public int Number { get; set; } 7 | public string RoomType { get; set; } 8 | public string BedSize { get; set; } 9 | 10 | public void Deconstruct(out string size, out string type) 11 | { 12 | size = BedSize; 13 | type = RoomType; 14 | } 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter08/Section-08-05/ScheduleType.cs: -------------------------------------------------------------------------------- 1 | namespace Section_08_05 2 | { 3 | public enum ScheduleType 4 | { 5 | None, 6 | Bronze, 7 | Silver, 8 | Gold 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter08/Section-08-05/Section-08-05.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | net7.0 6 | Section_08_05 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter08/Section-08-06/Room.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | namespace Section_08_06 3 | { 4 | public class Room 5 | { 6 | public int Number { get; set; } 7 | public string RoomType { get; set; } 8 | public string BedSize { get; set; } 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter08/Section-08-06/ScheduleType.cs: -------------------------------------------------------------------------------- 1 | namespace Section_08_06 2 | { 3 | public enum ScheduleType 4 | { 5 | None, 6 | Bronze, 7 | Silver, 8 | Gold 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter08/Section-08-06/Section-08-06.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | net7.0 6 | Section_08_06 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter08/Section-08-07/BronzeSchedule.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | namespace Section_08_07 3 | { 4 | public class BronzeSchedule : IRoomSchedule 5 | { 6 | public void ScheduleRoom() => 7 | Console.WriteLine("Scheduling Bronze Room"); 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter08/Section-08-07/GoldSchedule.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | namespace Section_08_07 3 | { 4 | public class GoldSchedule : IRoomSchedule 5 | { 6 | public void ScheduleRoom() => 7 | Console.WriteLine("Scheduling Gold Room"); 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter08/Section-08-07/IRoomSchedule.cs: -------------------------------------------------------------------------------- 1 | namespace Section_08_07 2 | { 3 | public interface IRoomSchedule 4 | { 5 | void ScheduleRoom(); 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter08/Section-08-07/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace Section_08_07 4 | { 5 | class Program 6 | { 7 | const int SilverPoints = 5000; 8 | const int GoldPoints = 20000; 9 | 10 | static void Main() 11 | { 12 | Console.Write("How many points? "); 13 | string response = Console.ReadLine(); 14 | 15 | if (!int.TryParse(response, out int points)) 16 | { 17 | Console.WriteLine($"'{response}' is invalid!"); 18 | return; 19 | } 20 | 21 | IRoomSchedule schedule = GetSchedule(points); 22 | 23 | schedule.ScheduleRoom(); 24 | } 25 | 26 | static IRoomSchedule GetSchedule(int points) => 27 | points switch 28 | { 29 | >= GoldPoints => new GoldSchedule(), 30 | >= SilverPoints => new SilverSchedule(), 31 | < SilverPoints => new BronzeSchedule() 32 | }; 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter08/Section-08-07/Section-08-07.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | net7.0 6 | Section_08_07 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter08/Section-08-07/SilverSchedule.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | namespace Section_08_07 3 | { 4 | public class SilverSchedule : IRoomSchedule 5 | { 6 | public void ScheduleRoom() => 7 | Console.WriteLine("Scheduling Silver Room"); 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter08/Section-08-08/BronzeSchedule.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | namespace Section_08_08 3 | { 4 | public class BronzeSchedule : IRoomSchedule 5 | { 6 | public void ScheduleRoom() => 7 | Console.WriteLine("Scheduling Bronze Room"); 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter08/Section-08-08/Customer.cs: -------------------------------------------------------------------------------- 1 | namespace Section_08_08 2 | { 3 | public class Customer 4 | { 5 | public int Points { get; set; } 6 | 7 | public bool HasFreeUpgrade { get; set; } 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter08/Section-08-08/GoldSchedule.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | namespace Section_08_08 3 | { 4 | public class GoldSchedule : IRoomSchedule 5 | { 6 | public void ScheduleRoom() => 7 | Console.WriteLine("Scheduling Gold Room"); 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter08/Section-08-08/IRoomSchedule.cs: -------------------------------------------------------------------------------- 1 | namespace Section_08_08 2 | { 3 | public interface IRoomSchedule 4 | { 5 | void ScheduleRoom(); 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter08/Section-08-08/Section-08-08.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | net7.0 6 | Section_08_08 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter08/Section-08-08/SilverSchedule.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | namespace Section_08_08 3 | { 4 | public class SilverSchedule : IRoomSchedule 5 | { 6 | public void ScheduleRoom() => 7 | Console.WriteLine("Scheduling Silver Room"); 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter08/Section-08-09/Customer.cs: -------------------------------------------------------------------------------- 1 | namespace Section_08_09 2 | { 3 | public class Customer 4 | { 5 | public int Points { get; set; } 6 | 7 | public int Month { get; set; } 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter08/Section-08-09/Section-08-09.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | net7.0 6 | Section_08_09 7 | 8 | 9 | 10 | latest 11 | 12 | 13 | latest 14 | 15 | 16 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter08/Section-08-10/BronzeCustomer.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | namespace Section_08_10 3 | { 4 | public class BronzeCustomer : Customer {} 5 | } 6 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter08/Section-08-10/BronzeSchedule.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | namespace Section_08_10 3 | { 4 | public class BronzeSchedule : IRoomSchedule 5 | { 6 | public void ScheduleRoom() => 7 | Console.WriteLine("Scheduling Bronze Room"); 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter08/Section-08-10/Customer.cs: -------------------------------------------------------------------------------- 1 | namespace Section_08_10 2 | { 3 | public class Customer {} 4 | } 5 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter08/Section-08-10/GoldCustomer.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | namespace Section_08_10 3 | { 4 | public class GoldCustomer : Customer {} 5 | } 6 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter08/Section-08-10/GoldSchedule.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | namespace Section_08_10 3 | { 4 | public class GoldSchedule : IRoomSchedule 5 | { 6 | public void ScheduleRoom() => 7 | Console.WriteLine("Scheduling Gold Room"); 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter08/Section-08-10/IRoomSchedule.cs: -------------------------------------------------------------------------------- 1 | namespace Section_08_10 2 | { 3 | public interface IRoomSchedule 4 | { 5 | void ScheduleRoom(); 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter08/Section-08-10/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | 4 | namespace Section_08_10 5 | { 6 | class Program 7 | { 8 | static void Main() 9 | { 10 | foreach (var customer in GetCustomers()) 11 | { 12 | IRoomSchedule schedule = GetSchedule(customer); 13 | schedule.ScheduleRoom(); 14 | } 15 | } 16 | 17 | static IRoomSchedule GetSchedule(Customer customer) => 18 | customer switch 19 | { 20 | GoldCustomer => new GoldSchedule(), 21 | SilverCustomer => new SilverSchedule(), 22 | BronzeCustomer => new BronzeSchedule(), 23 | _ => new BronzeSchedule() 24 | }; 25 | 26 | static List GetCustomers() => 27 | new List 28 | { 29 | new GoldCustomer(), 30 | new SilverCustomer(), 31 | new BronzeCustomer() 32 | }; 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter08/Section-08-10/Section-08-10.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | net7.0 6 | Section_08_10 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter08/Section-08-10/SilverCustomer.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | namespace Section_08_10 3 | { 4 | public class SilverCustomer : Customer {} 5 | } 6 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter08/Section-08-10/SilverSchedule.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace Section_08_10 4 | { 5 | public class SilverSchedule : IRoomSchedule 6 | { 7 | public void ScheduleRoom() => 8 | Console.WriteLine("Scheduling Silver Room"); 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter09/Section-09-01/AnotherFile.cs: -------------------------------------------------------------------------------- 1 | //using System; 2 | 3 | // not allowed 4 | //Console.WriteLine("I'm in AnotherFile.cs"); -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter09/Section-09-01/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | Console.WriteLine("Address Info:\n"); 4 | 5 | Console.Write("Street: "); 6 | string street = Console.ReadLine(); 7 | 8 | Console.Write("City: "); 9 | string city = Console.ReadLine(); 10 | 11 | Console.Write("State: "); 12 | string state = Console.ReadLine(); 13 | 14 | Console.Write("Zip: "); 15 | string zip = Console.ReadLine(); 16 | 17 | Console.WriteLine($@" 18 | Your address is: 19 | 20 | {street} 21 | {city}, {state} {zip}"); -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter09/Section-09-01/Section-09-01.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | net7.0 6 | Section_09_01 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter09/Section-09-02/Address.cs: -------------------------------------------------------------------------------- 1 | namespace Section_09_02 2 | { 3 | public class Address 4 | { 5 | public Address() { } 6 | 7 | public Address( 8 | string street, 9 | string city, 10 | string state, 11 | string zip) 12 | { 13 | Street = street; 14 | City = city; 15 | State = state; 16 | Zip = zip; 17 | } 18 | 19 | public string Street { get; set; } 20 | public string City { get; set; } 21 | public string State { get; set; } 22 | public string Zip { get; set; } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter09/Section-09-02/Program.cs: -------------------------------------------------------------------------------- 1 | namespace Section_09_02 2 | { 3 | class Program 4 | { 5 | // doesn't work at this level 6 | // var address = new Address(); 7 | 8 | // this still works 9 | Address addressOld = new Address(); 10 | 11 | // new target typed field 12 | Address addressNew = new(); 13 | 14 | static void Main() 15 | { 16 | // these still work 17 | var addressLocalVar = new Address(); 18 | Address addressLocalOld = new Address(); 19 | 20 | // new target typed local variable 21 | Address addressLocalNew = new(); 22 | 23 | // target typed with object ini 24 | Address addressObjectInit = new() 25 | { 26 | Street = "123 4th St.", 27 | City = "My City", 28 | State = "ZZ", 29 | Zip = "55555-3333" 30 | }; 31 | 32 | // target typed with ctor init 33 | Address addressCtorInit = new( 34 | street: "567 8th Ave.", 35 | city: "Some Place", 36 | state: "YY", 37 | zip: "12345-7890"); 38 | } 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter09/Section-09-02/Section-09-02.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | net7.0 6 | Section_09_02 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter09/Section-09-03/Address.cs: -------------------------------------------------------------------------------- 1 | namespace Section_09_03 2 | { 3 | public class Address 4 | { 5 | public Address() { } 6 | 7 | public Address( 8 | string street, 9 | string city, 10 | string state, 11 | string zip) 12 | { 13 | Street = street; 14 | City = city; 15 | State = state; 16 | Zip = zip; 17 | } 18 | 19 | public string Street { get; init; } 20 | public string City { get; init; } 21 | public string State { get; init; } 22 | public string Zip { get; init; } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter09/Section-09-03/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace Section_09_03 4 | { 5 | class Program 6 | { 7 | static void Main(string[] args) 8 | { 9 | Address addressObjectInit = new() 10 | { 11 | Street = "123 4th St.", 12 | City = "My City", 13 | State = "ZZ", 14 | Zip = "55555-3333" 15 | }; 16 | 17 | // not allowed 18 | //addressObjectInit.City = "A Locality"; 19 | 20 | // target typed with ctor init 21 | Address addressCtorInit = new( 22 | street: "567 8th Ave.", 23 | city: "Some Place", 24 | state: "YY", 25 | zip: "12345-7890"); 26 | 27 | // not allowed 28 | //addressCtorInit.Zip = "98765"; 29 | } 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter09/Section-09-03/Section-09-03.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | net7.0 6 | Section_09_03 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter09/Section-09-04/Section-09-04.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | net7.0 6 | Section_09_04 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter09/Section-09-05/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace Section_09_05 4 | { 5 | record Address( 6 | string Street, 7 | string City, 8 | string State, 9 | string Zip); 10 | 11 | class Program 12 | { 13 | static void Main(string[] args) 14 | { 15 | Address addressPre = new( 16 | Street: "567 8th Ave.", 17 | City: "Some Place", 18 | State: "YY", 19 | Zip: "12345-7890"); 20 | 21 | Address addressPost = 22 | addressPre with 23 | { 24 | Street = "569 8th Ave." 25 | }; 26 | 27 | Console.WriteLine($"Pre: {addressPre}"); 28 | Console.WriteLine($"Post: {addressPost}"); 29 | 30 | Console.WriteLine( 31 | $"Value Equal: " + 32 | $"{addressPre == addressPost}"); 33 | } 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter09/Section-09-05/Section-09-05.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | net7.0 6 | Section_09_05 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter09/Section-09-06/AddressBase.cs: -------------------------------------------------------------------------------- 1 | namespace Section_09_06 2 | { 3 | public abstract record AddressBase( 4 | string Street, 5 | string City, 6 | string State, 7 | string Zip); 8 | } 9 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter09/Section-09-06/MailingAddress.cs: -------------------------------------------------------------------------------- 1 | namespace Section_09_06 2 | { 3 | public record MailingAddress( 4 | string Street, 5 | string City, 6 | string State, 7 | string Zip, 8 | string Email, 9 | bool PreferEmail) 10 | : AddressBase(Street, City, State, Zip); 11 | } 12 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter09/Section-09-06/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace Section_09_06 4 | { 5 | class Program 6 | { 7 | static void Main(string[] args) 8 | { 9 | MailingAddress mailAddress = new( 10 | Street: "567 8th Ave.", 11 | City: "Some Place", 12 | State: "YY", 13 | Zip: "12345-7890", 14 | Email: "me@example.com", 15 | PreferEmail: true); 16 | 17 | ShippingAddress shipAddress = new( 18 | street: "567 8th Ave.", 19 | city: "Some Place", 20 | state: "YY", 21 | zip: "12345-7890", 22 | deliveryInstructions: "Ring Doorbell"); 23 | 24 | Console.WriteLine($"Mail: {mailAddress}"); 25 | Console.WriteLine($"Ship: {shipAddress}"); 26 | 27 | Console.WriteLine( 28 | $"Derived types equal: " + 29 | $"{mailAddress == shipAddress}"); 30 | 31 | AddressBase mailBase = mailAddress; 32 | AddressBase shipBase = shipAddress; 33 | Console.WriteLine( 34 | $"Base types equal: " + 35 | $"{mailBase == shipBase}"); 36 | } 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter09/Section-09-06/Section-09-06.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | net7.0 6 | Section_09_06 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter09/Section-09-06/ShippingAddress.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace Section_09_06 4 | { 5 | public record ShippingAddress : AddressBase 6 | { 7 | public ShippingAddress( 8 | string street, 9 | string city, 10 | string state, 11 | string zip, 12 | string deliveryInstructions) 13 | : base(street, city, state, zip) 14 | { 15 | if (street.Contains("P.O. Box")) 16 | throw new ArgumentException( 17 | "P.O. Boxes aren't allowed"); 18 | 19 | DeliveryInstructions = deliveryInstructions; 20 | } 21 | 22 | public string DeliveryInstructions { get; init; } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter09/Section-09-07/AddressBase.cs: -------------------------------------------------------------------------------- 1 | namespace Section_09_07 2 | { 3 | public abstract record AddressBase( 4 | string Street, 5 | string City, 6 | string State, 7 | string Zip); 8 | } 9 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter09/Section-09-07/Communications.cs: -------------------------------------------------------------------------------- 1 | namespace Section_09_07 2 | { 3 | class Communications : DeliveryBase 4 | { 5 | public override MailingAddress GetAddress(string name) 6 | { 7 | return new( 8 | Street: "567 8th Ave.", 9 | City: "Some Place", 10 | State: "YY", 11 | Zip: "12345-7890", 12 | Email: "me@example.com", 13 | PreferEmail: true); 14 | } 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter09/Section-09-07/DeliveryBase.cs: -------------------------------------------------------------------------------- 1 | namespace Section_09_07 2 | { 3 | abstract class DeliveryBase 4 | { 5 | public abstract AddressBase GetAddress(string name); 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter09/Section-09-07/MailingAddress.cs: -------------------------------------------------------------------------------- 1 | namespace Section_09_07 2 | { 3 | public record MailingAddress( 4 | string Street, 5 | string City, 6 | string State, 7 | string Zip, 8 | string Email, 9 | bool PreferEmail) 10 | : AddressBase(Street, City, State, Zip); 11 | } 12 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter09/Section-09-07/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace Section_09_07 4 | { 5 | class Program 6 | { 7 | static void Main(string[] args) 8 | { 9 | Communications comm = new(); 10 | MailingAddress mailAddr = comm.GetAddress("Person A"); 11 | Console.WriteLine(mailAddr); 12 | 13 | Shipping ship = new(); 14 | ShippingAddress shipAddr = ship.GetAddress("Person B"); 15 | Console.WriteLine(shipAddr); 16 | } 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter09/Section-09-07/Section-09-07.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | net7.0 6 | Section_09_07 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter09/Section-09-07/Shipping.cs: -------------------------------------------------------------------------------- 1 | namespace Section_09_07 2 | { 3 | class Shipping : DeliveryBase 4 | { 5 | public override ShippingAddress GetAddress(string name) 6 | { 7 | return new( 8 | street: "567 8th Ave.", 9 | city: "Some Place", 10 | state: "YY", 11 | zip: "12345-7890", 12 | deliveryInstructions: "Ring Doorbell"); 13 | } 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter09/Section-09-07/ShippingAddress.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace Section_09_07 4 | { 5 | public record ShippingAddress : AddressBase 6 | { 7 | public ShippingAddress( 8 | string street, 9 | string city, 10 | string state, 11 | string zip, 12 | string deliveryInstructions) 13 | : base(street, city, state, zip) 14 | { 15 | if (street.Contains("P.O. Box")) 16 | throw new ArgumentException( 17 | "P.O. Boxes aren't allowed"); 18 | 19 | DeliveryInstructions = deliveryInstructions; 20 | } 21 | 22 | public string DeliveryInstructions { get; init; } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter09/Section-09-08/Address.cs: -------------------------------------------------------------------------------- 1 | namespace Section_09_08 2 | { 3 | public record Address( 4 | string Street, 5 | string City, 6 | string State, 7 | string Zip); 8 | } 9 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter09/Section-09-08/AddressExtensions.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | 3 | namespace Section_09_08 4 | { 5 | public static class AddressExtensions 6 | { 7 | public static IEnumerator GetEnumerator( 8 | this Address address) 9 | { 10 | yield return address.Street; 11 | yield return address.City; 12 | yield return address.State; 13 | yield return address.Zip; 14 | yield break; 15 | } 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter09/Section-09-08/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | 4 | namespace Section_09_08 5 | { 6 | class Program 7 | { 8 | static void Main() 9 | { 10 | IEnumerable
addresses = GetAddresses(); 11 | 12 | foreach (var address in addresses) 13 | { 14 | foreach (var line in address) 15 | Console.WriteLine(line); 16 | 17 | Console.WriteLine(); 18 | } 19 | } 20 | 21 | static IEnumerable
GetAddresses() 22 | { 23 | return new List
24 | { 25 | new Address( 26 | Street: "567 8th Ave.", 27 | City: "Some Place", 28 | State: "YY", 29 | Zip: "12345-7890"), 30 | new Address( 31 | Street: "569 8th Ave.", 32 | City: "Some Place", 33 | State: "YY", 34 | Zip: "12345-7890") 35 | }; 36 | } 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter09/Section-09-08/Section-09-08.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | net7.0 6 | Section_09_08 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter09/Section-09-09/Address.cs: -------------------------------------------------------------------------------- 1 | namespace Section_09_09 2 | { 3 | public record Address( 4 | string Street, 5 | string City, 6 | string State, 7 | string Zip); 8 | } 9 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter09/Section-09-09/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace Section_09_09 4 | { 5 | class Program 6 | { 7 | static void Main() 8 | { 9 | AddressService addressSvc = new(); 10 | 11 | foreach (var addresses in 12 | addressSvc.GetAddresses(perPage: 3)) 13 | { 14 | foreach (var address in addresses) 15 | { 16 | Console.WriteLine(address); 17 | } 18 | 19 | Console.WriteLine("\nNew Page\n"); 20 | } 21 | } 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter09/Section-09-09/Section-09-09.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | net7.0 6 | Section_09_09 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter09/Section-09-10/AddressManager/AddressManager.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | net7.0 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter09/Section-09-10/AddressManager/Program.cs: -------------------------------------------------------------------------------- 1 | using AddressUtilities; 2 | using System; 3 | 4 | namespace AddressManager 5 | { 6 | class Program 7 | { 8 | static void Main() 9 | { 10 | AddressService addressSvc = AddressService.Create(); 11 | 12 | addressSvc 13 | .GetAddresses() 14 | .ForEach(address => 15 | Console.WriteLine(address)); 16 | } 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter09/Section-09-10/AddressUtilities/Address.cs: -------------------------------------------------------------------------------- 1 | namespace AddressUtilities 2 | { 3 | public record Address( 4 | string Street, 5 | string City, 6 | string State, 7 | string Zip); 8 | } 9 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter09/Section-09-10/AddressUtilities/AddressRepository.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | 3 | namespace AddressUtilities 4 | { 5 | public class AddressRepository : IAddressRepository 6 | { 7 | public List
GetAddresses() => 8 | new List
9 | { 10 | new ( 11 | Street: "123 4th St.", 12 | City: "My Place", 13 | State: "ZZ", 14 | Zip: "12345-7890"), 15 | new ( 16 | Street: "567 8th Ave.", 17 | City: "Some Place", 18 | State: "YY", 19 | Zip: "12345-7890"), 20 | new ( 21 | Street: "567 8th Ave.", 22 | City: "Some Place", 23 | State: "YY", 24 | Zip: "12345-7890") 25 | }; 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter09/Section-09-10/AddressUtilities/AddressService.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.Extensions.DependencyInjection; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | 5 | namespace AddressUtilities 6 | { 7 | public class AddressService 8 | { 9 | readonly IAddressRepository addressRep; 10 | 11 | public AddressService(IAddressRepository addressRep) => 12 | this.addressRep = addressRep; 13 | 14 | public static AddressService Create() => 15 | Initializer.Container.GetRequiredService(); 16 | 17 | public List
GetAddresses() => 18 | (from address in addressRep.GetAddresses() 19 | select address) 20 | .Distinct() 21 | .ToList(); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter09/Section-09-10/AddressUtilities/AddressUtilities.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | net7.0 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter09/Section-09-10/AddressUtilities/IAddressRepository.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | 3 | namespace AddressUtilities 4 | { 5 | public interface IAddressRepository 6 | { 7 | List
GetAddresses(); 8 | } 9 | } -------------------------------------------------------------------------------- /CSharp9Cookbook/Chapter09/Section-09-10/AddressUtilities/Initializer.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.Extensions.DependencyInjection; 2 | using System.Runtime.CompilerServices; 3 | 4 | namespace AddressUtilities 5 | { 6 | class Initializer 7 | { 8 | internal static ServiceProvider Container { get; set; } 9 | 10 | [ModuleInitializer] 11 | internal static void InitAddressUtilities() 12 | { 13 | var services = new ServiceCollection(); 14 | services.AddTransient(); 15 | services.AddTransient(); 16 | Container = services.BuildServiceProvider(); 17 | } 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /CSharp9Cookbook/OrdersLibrary.Test/OrdersLibrary.Test.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | netcoreapp3.1 5 | 6 | false 7 | 8 | 9 | 10 | 11 | 12 | 13 | runtime; build; native; contentfiles; analyzers; buildtransitive 14 | all 15 | 16 | 17 | runtime; build; native; contentfiles; analyzers; buildtransitive 18 | all 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2021 Joe Mayo 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # csharp-nine-cookbook --------------------------------------------------------------------------------