Thursday, August 8, 2024

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 class or struct declaration. 

There are two ways to declare records 

public record class Person(string FirstName,  int Age);

or we can define properties explicitly like

public record class Person{

    public string Name{get;set;}

    public int Age{get;set;}

}

var p1=new Person("Niranjan", 30);

var p2=new Person("Niranjan",30)

if we will compare p1 and p2 ,it will return true.

if(p1==p2) //true ; while in case of class it will return false.

Points to remember:

  • If records declared with primary constructor syntax, they will be immutable. we can change properties and get new record instance using 'with' keywords.
  • record overides ToString(),ToEqual(), GetHashCode() and implements destructing.
  • using .ToString with records prints all properties with name and value.
  •  record overriddes ToEqual() methods uses IEquatable<T> interface to compare preparties values so it's necessary to use property datatype which implements IEquatable<T> in order to get successful value comparison for two records.
Example:

public records class Employee(string Name, int Age, List<string> Addresses);

var e1=new Employee("Raj",30,new List<string>{"A1","A2"});
var e2=new Employee("Raj",30,new List<string>{"A1","A2"});

if(e1==e2) // will return false as List<string> does not implement IEquatable<T> interface. 

Saturday, July 20, 2024

difference between Pub/Sub and observer pattern

The purpose of the Publish/Subscribe pattern is the same as the Observer pattern: you want to notify other services when certain events take place. But there is an important difference between the Observer and Pub/Sub patterns. In the observer pattern, the broadcast is performed directly from the observable to the observers, so they "know" each other. But when using a Pub/Sub pattern, there is a third component, called broker, or message broker or event bus, which is known by both the publisher and subscriber. Therefore, when using the Pub/Sub pattern the publisher and the subscribers are precisely decoupled thanks to the mentioned event bus or message broker.

Thursday, May 16, 2024

Difference between First, FirstOrDefault, Single, SingleOrDefault

 

See the below example I am adding 4 items to list with 1 duplicate and calling these methods, output written with comments side by method call.

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

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

            var findfirst= lst.Find(x => x.Id == 9);

            var firstElement = lst.First(x => x.Id == 9);//exception

            var firstOrDefaultTest = lst.FirstOrDefault(x => x.Id == 90); //null, default value;

            var singleOrdefaulttest = lst.SingleOrDefault(x => x.Id == 1);// exception because found more than 1 element for Id=1. no exception In-case not found.

            var singleTest = lst.Single(x => x.Id == 90);//exception if not found also if found duplicate.

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

        }

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