Thursday, May 16, 2024

Something about List item reference

     // Some differenent example 


            Base b1 = new Base() { Id = 1, Name = "base1" };

            Base b2 = new Base() { Id =2, Name = "base2" };

            Base b3 = new Base() { Id = 3, Name = "base3" };

            List<Base> lst = new List<Base> { b1, b2, b3 }; //b1 ,b2, b3 added to list

            Base b4 = new Base { Id = 4, Name = "base4" };

            PRintList(lst);


            var first= lst.Find(x => x.Id == 1); // fetching first item and chaging it by assigning new item.

            first = b4; //it will not affect list item

            PRintList(lst);

            

            lst[0] = b4; // now lost[0] will be b4.

            PRintList(lst);



            void PRintList(List<Base> lst)

            {

                foreach(Base b in lst) Console.WriteLine(   b.Id +" -"+ b.Name);

            }


            //Output:

            //1 - base1

            //2 - base2

            //3 - base3


            //1 - base1

            //2 - base2

            //3 - base3


            //4 - base4

            //2 - base2

            //3 - base3

Sunday, May 12, 2024

Null conditional Operator, Null Coalescing Operator, Null Forgiving Operator in C#

  

Null Conditional Operator (?): Generally used with Object reference variable before accessing any property of that object, it checks that if object is null then it will return null without accessing any property of that object and prevent from object reference exception. for example,

var customerName = objCustomer?.Name ;

Above expression return null if objCustomer is null without accessing Name property of Customer.

Null-Coalescing Operator (??): If applied variable is null then it will return right hand side given value else it will return value of variable. for example,

var customerName=objCustomer?.Name?? "Not Found";

If customer has no value (null) for its name, then above expression will return "Not Found" else name of customer.

Null Forgiving Operator (!): If Nullable enabled in project file means you are using null state analysis feature for framework, which allow you to define reference type either nullable or non- nullable.  If non nullable reference type contains null compiler will generate warning. sometimes we know that this variable is nullable variable, and it has non null value, but compiler still force you to check for null, in this case you can use (!) null forgiving operator to tell that I know it's not null, don't generate warning. 

 Product?[] products = Product.Get(); 

 var productName= products[0]!.Name ;

Note: You can disable null state analysis on specific file by using " #pragma warning disable CS8602.


How to declare Nullable reference type in c#

 A variable of type Product?[] denotes an array that can contain Product or null values but that won’t be null itself:

Product?[] arr1 = new Product?[] { p1, p2, null }; // OK

Product?[] arr2 = null; // Not OK

A variable of type Product[]? is an array that can hold only Product values and not null values, but the array itself may be null:

Product[]? arr1 = new Product[]? { p1, p2, null }; // Not OK

Product[]? arr2 = null; // OK

A variable of type Product?[]? is an array that can contain Product or null values and that can itself be null:

Product?[]? arr1 = new Product?[] {p1, p2, null }; // OK

Product?[]? arr2 = null; // Also OK


Sunday, May 5, 2024

Using SortedList in C#

Sorted list in C# is a collection of Key-Value pairs, Values are accessed via Key or by Index. It's generic so we can choose any datatype for Key. Key must be unique while we can have duplicate values. Key are sorted automatically, due to this it's a bit slower as compared to HashTable or Dictionary.

Removing or adding a item in sorted list will change index of items as it will sort keys.

Below are some important methods exposed by SortedList. 

        public TKey GetKeyAtIndex(int index);

        public TValue GetValueAtIndex(int index);

        public int IndexOfKey(TKey key);

        public int IndexOfValue(TValue value);

public void SetValueAtIndex(int index, TValue value);

Example: Copy and paste below code in VSCode/VisualStudio

 public static void WorkingWithSortedList()

        {

            //Soreted list is a collection of sorted key value pair.

            // data can be accessed via key or index.

            var list = new SortedList<int, string>();

            list.Add(1, "One");

            list.Add(2, "Two");

            list.Add(3, "Three");

           // list.Add(2, "Two"); //Duplicates are not allowed.

            list.Add(5, "Five");

            list.Add(6, "Six");

            list.Add(4, "Four");//Adding at last but it would get added at index (3);

            list.Add(7, "One"); // Duplicate values are allowed!

            Console.WriteLine(list.Count);

            Console.WriteLine("Accessiing sorted list elements");

            //Below loop will print item which is Key-value pair object

            foreach (var item in list)

            {                Console.WriteLine("Sorted list is a collection of KeyValue :" + item);            }

           foreach (var item in list)  

          {          Console.WriteLine($"item Key={item.Key}, Value={item.Value}");            }

            for(int i = 0; i < list.Count; i++)  {

                Console.WriteLine($"Item at index {i}={list.GetKeyAtIndex(i)} ,{list.GetValueAtIndex(i)}");

            }

            //Chning item for key=2

            list[2] = "Two Changed";

           foreach (var item in list)        {

                Console.WriteLine(item);

                Console.WriteLine($"item Key={item.Key}, Value={item.Value}");

           }

            Console.WriteLine(  "Getting index of last added item i.e.4");

            Console.WriteLine( list.IndexOfKey(4));

            Console.WriteLine("Getting index of value Four");

            Console.WriteLine(list.IndexOfValue("Four"));

            Console.WriteLine("Index of above key-value would get changed as removing itewm from index 2");

             list.Remove(2); //removed item by key;

             Console.WriteLine("Getting index of last added item i.e.4");

            Console.WriteLine(list.IndexOfKey(4));

            Console.WriteLine("Getting index of value Four");

            Console.WriteLine(list.IndexOfValue("Four"));

            Console.WriteLine(  "***********Prining Keys ****************");

            foreach(var key in list.Keys) { Console.WriteLine(key); }

            Console.WriteLine("***********Prining Values ****************");

            foreach (var val in list.Values) { Console.WriteLine(val); }

            Console.WriteLine(" WHat if trying to access key which is not present");

         //   Console.WriteLine(list[8]); // Thow exception as no item at index 8

        }

Friday, March 1, 2024

C# : Merge two sorted array

 int [] first=new int[]{1,3,5,9,11,15};

int [] second=new int[]{2,4,6,8};  

int n1=first.Length, n2=second.Length;
int [] third =new int[n1+n2];
Console.WriteLine($" n1={n1} and n2={n2}");
int i=0,j=0,k=0;

// Logic
// ==>i=0;j=0 =>1
// i=1,j=0 =>2
// i=1,j=1=>4
// i=1, j=2 => 5
// i=2, j=2 ==>6
// i=2, j=3 ==>8
// i=2,j=4 ==> exit lpoop ==>

while(i<n1 &&j<n2){  
    Console.WriteLine($" i={i} , j={j}, k={k}");
    if(first[i]>second[j]){
        third[k++]=second[j++];      
    }
    else{
        third[k++]=first[i++];        
    }    
}
while(i<n1){
    third[k++]=first[i++];
}
while(j<n2){
    third[k++]=second[j++];
}
foreach (var item in third)
{
    Console.WriteLine(item +"--");
}

Friday, July 2, 2021

Difference between object and dynamic in c# | Object Vs Dynamic

As we know that object is a base class for the the types in c# i.e. object is a class and all the classes in .net framework derived from object. we can assign any value (either ValueType or ReferenceType) to object, similarly we can assign any value to the dynamic. but there are few difference in dynamic and object 
  • dynamic is not a type it's just a keyword which tells compiler to ignore compile time type checking and infer dynamic declared variable's type at runtime.
  • when you assign a value to dynamic variable and then you assign this variable's value to another variable who's type is similar to assigned value's type you don't need type casting (see the example below) but in case of object you need to type caste object variable to particular type otherwise you will get compile time error.
      public static void ObjectVsDynamic()
        {
            dynamic a = "hello";
            string str = a; // no casting
          
            Console.WriteLine(str);

            object b = "hello";
            string str1 = (string)b;// in case of object explicit casting required.
            Console.WriteLine(str1);
            Console.ReadLine();
        }

Tuesday, June 2, 2020

ASP.NET Core 3.0 (C# ) : Send email via outlook host

Below code shows how to send email via outlook host using SMTP client in C#

     public interface IOutlookMailClient
    {
        void SendMail(string recipient, string subject, string message);
    }

public class OutlookMailClient : IOutlookMailClient
    {
        private readonly string sender;
        private readonly string password;
        private readonly string host;
        private readonly int port;
        private readonly OutlookMailServiceOptions emailOptions;
        private readonly ILogger<OutlookMailClient> logger;
       
public OutlookMailClient(IOptions<OutlookMailServiceOptions>                   mailOptions,ILogger<OutlookMailClient> logger)
        {
            this.emailOptions = mailOptions.Value; ;
            this.password = emailOptions.Password;
            this.host = emailOptions.Host;
            this.port = emailOptions.Port;
            this.sender = emailOptions.Sender;
            this.logger = logger;
        }
        public void SendMail(string recipient, string subject, string message)
        {
            logger.LogInformation("Enter : OutlookMailClient.SendMail");
            logger.LogInformation("Mail recipient :" +recipient +" Subject : "+subject);
            SmtpClient client = new SmtpClient(host);//new SmtpClient("outlook.office365.com");
            client.Port = port;
            //client.Port = 587;
            client.DeliveryMethod = SmtpDeliveryMethod.Network;
            client.UseDefaultCredentials = false;
            System.Net.NetworkCredential credentials =
            new System.Net.NetworkCredential(sender, password);
            client.EnableSsl = true;
            client.Credentials = credentials;
            try
            {
                var mail = new MailMessage(sender.Trim(), recipient.Trim());
                mail.Subject = subject;
                mail.Body = message;
                client.Send(mail);
                logger.LogInformation("Exit : OutlookMailClient.SendMail");
            }
            catch (Exception ex)
            {
                logger.LogError(ex.Message);
                throw ex;
            }
        }
    }
}

IOptions<OutlookMailServiceOptions> is a asp.net core feature to read data from appsetting.json using a model. here OutlookMailServiceOptions is a POCO class used to keep mail settings from Json config file. you can use any alternative if you are not using asp.net core.

 public class OutlookMailServiceOptions
    {   
            public string Sender { get; set; }
            public string Host { get; set; }
            public int Port { get; set; }
            public string Password { get; set; }
    
    }
put this configuration in appsetting.json

"OutlookMailServiceOptions": {
    "Sender": "sender mail id",
    "Host": "outlook.office365.com",
    "Port": 587,
    "Password": "your password"
  }

Now you have to call this api in your code, to call first you need to inject this api via dependency injection in ConfigureService method of Startup.cs like :

 services.Configure<OutlookMailServiceOptions>(Configuration.GetSection(nameof(OutlookMailServiceOptions))).AddSingleton(x => x.GetRequiredService<IOptions<OutlookMailServiceOptions>>().Value);

            services.AddScoped<IOutlookMailClient, OutlookMailClient>();

Now, you can inject this API in your Controller via constructor :

private readonly IOutlookMailClient emailClient
DefaultController (IOutlookMailClient emailClient) //Constructor
{
this.emailClient=emailClient;
}
 
//mail api injected , now simply call sendMail() method from any method like:

  emailClient.SendMail("To mail address", "Subject", "Body");

All the other required details (Sender, Host, Port, etc.) to send mail, API will read from AppSetting.Json via IOption mechanism.

C# Record type: Something to remember while using record types

  Record in c# provide a concise and expressive way to create immutable data types, record is a keyword in c#, we can use this keyword with ...