Posts

To read

Options pattern   https://learn.microsoft.com/en-us/aspnet/core/fundamentals/configuration/options?view=aspnetcore-7.0 OptionMonitor OptionSnapshot IConfiguration Option -> once application is loaded the value will not change. The application has to be restarted. Aws Systems Manager - create parameter store https://www.youtube.com/watch?v=J0EVd5HbtUY

Reading config from a c# console application

 Console application 1. Install nuget package Microsoft.Extensions.Hosting 2.  using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Configuration; using IHost host = Host.CreateDefaultBuilder(args).Build(); IConfiguration config = new ConfigurationBuilder()     .AddJsonFile("appSettings.json")     .AddEnvironmentVariables()     .Build(); Settings settings = config.GetRequiredSection(key: "Settings").Get<Settings>(); Console.WriteLine(settings.KeyOne); Console.WriteLine(settings.KeyTwo); Console.WriteLine(settings.KeyThree.Message); foreach(var ipAddress in settings.IPAddresses) {     Console.WriteLine(ipAddress); } foreach(var versions in settings.KeyThree.SupportedVersions) {     Console.WriteLine(versions.Key);     Console.WriteLine(versions.Value); } Console.WriteLine(settings.KeyThree.SupportedVersions); await host.RunAsync(); in the properties of the project add the following. <ItemGroup> ...

Record Type in C#

 Record type Record can be class or struct. Class is default and it need not be defined. public record class Person (string FullName, DateOnly DateOfBirth); equivalent to saying public class Person{ public string FullName {get; init;} = default!; public DateOnly DateOfBirth {get; set;} } public record struct Person(string FullName, DateOnly DateOfBirth); Console.WriteLine(new Person{ FullName = "Kirthiga", DateOfBirth = new DateOnly(1996, 8, 7)}; It will say NameSpace.Person but won't say the details var kirthiga = new Person("Kirthiga", new DateOnly(1996,8,7)); On the other hand when you use record and do a Console.WriteLine(kirthiga); It will print out Person {FullName = "Kirthiga", DateOfBirth = "07/08/1996"} with full details Also when we duplicate classes example, var personA = new Person{ FullName = "Kirthiga", DateOfBirth = "07/08/1996" }  var personB = new Person{ FullName = "Kirthiga", DateOfBirth = ...

How to find out which version of C# is used in a project in Visual Studio 2022

Image
  Right-click Project -> Properties -> Build -> Advanced -> Language version

How to find the C# version being used in your system while using Visual Studio 2022

  If you are using Visual Studio 2022, Right-click project -> Properties -> Build -> Advanced -> Language Version

How to pass command line arguments in Visual Studio 2022?

Image
How to pass command line arguments in Visual Studio 2022? Right-click Projects -> Properties  In the window select Debug -> General -> Open debug launch profiles UI How to access the command line variables when top-level statements is used? Console.WriteLine("Hello, World!"); foreach(var s in args) {     Console.Write($"{s}, ");    } For more information about top-level statements follow this url: https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/tutorials/top-level-statements
  Design patterns The Active Record design pattern is a technique to save data from in-memory objects to the database, and load data from the database into in-memory objects. This pattern includes the code for saving and loading inside the model class.(Single Responsibility Principle is violated, as this single class holds both database updates and also the model. There is more than one reason for this class to change.  The Data Mapper Design Pattern is a similar pattern but moves the saving and loading code out of the model, into a separate class.