Posts

LINQ (Where, Min, Max, Average)

Image
 LINQ in C# Chaining using System; using System.Linq; public class Program { public static void Main() { string[] names = { "Tom", "Radha" , "Madhusudan", "Madhan" ,"Harry" , "Michael" }; var filteredNames = names.Where(n => n.Length > 4).OrderByDescending(n=>n); IEnumerable<string> filteredNames1 = System.Linq.Enumerable.Where(names, n => n.Contains("rr")); IEnumerable<string> filteredNames1 = System.Linq.Enumerable.Where(names, n => n.Contains('a')); Console.WriteLine(names.Min((n => n.Length))); Console.WriteLine(names.Max((n => n.Length))); Console.WriteLine(names.Average((n => n.Length))); Console.WriteLine("-----"); var query = from name in names where name.Length > 3         select name; foreach(var name in query){ Console.WriteLine(name); } Console.WriteLine("-----"); foreach(var name in ...

Delegates and Lambda Expressions notes

 Delegate: pointer to a function Eg. Photo class -> Load and Save methods Photofilters -> Contains different types of filters. Eg., BWFilter, Contrast, Brightness, Resize etc PhotoProcessor -> Takes the path, loads a photo, applies some filters and saves the Photo. If a developer wants to develop a new filter, he has to download the code, create the filter and then make changes to PhotoProcessor. Instead of PhotoProcessor taking the actual filters class, will take a PhotoFilterHandler delegate and will take as parameter. public delegate PhotoFilterHandler(Photo photo); public Photo process(string path, PhotoFilterHandler filter){ photo.Load(path);  filter(photo); photo.Save(); } while implementing it create an instance photoprocessor class. Create an instance of PhotoFilterHandler var filterHandler = photoFilter.ApplyBWFilter; filterHandler += photoFilter.ApplyBrightnessFilter; filterHandler += Resize; photoProcessor.process("c:\\photos\\photo1.png", filterHandler); ...

Connect to Sql Database and get data C#

Download Nuget package Mysql.Data Import namespace MySql.Data.MySqlClient Construct the connectionstring eg., server=localhost;user=root;password=secret;database=myDatabase Employees has three rows  EmployeeId, FirstName and LastName var connectionString = $"server={dbCon.Server};user={dbCon.UserName};password={dbCon.Password};database={dbCon.DatabaseName}"; using var connection = new MySqlConnection(connectionString); connection.Open(); string query = "select * from Employees";     var cmd = new MySqlCommand(query, connection);     var reader = cmd.ExecuteReader();     while (reader.Read()){         Console.WriteLine($"{reader.GetString(0)} {reader.GetString(1)} {reader.GetString(2)}");     }

Get HMAC of a string

Get HMAC of a string using System; using System.Text; using System.Security.Cryptography; public class Program { public static void Main() { Console.WriteLine(GetHMAC("abdfasdfsdfc","fsdafasdfabc")); } private  static string GetHMAC(string text, string key){ using (var hmacsha256 = new HMACSHA256(Encoding.UTF8.GetBytes(key))){ var hash = hmacsha256.ComputeHash(Encoding.UTF8.GetBytes(text)); return Convert.ToBase64String(hash); } } }

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 = ...