Posts

Showing posts from February, 2023

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); } } }