Asynchronous Programming - System.Threading.Thread - Part 2

  • Thread
  • The Thread class was, originally, a 1:1 mapping to an operating system thread.
  • It is typically used for long-running or specialized work such as monitoring a device or executing code with a low priority.
  • Using the Thread class leaves us with a lot of control over the thread.
  • Thread Class
  • The Thread class is sealed
  • Creating and Starting a Thread
  •  

Asynchronous Programming - Intro - Part 1

  • C# supports parallel execution of code through multi-threading
  • Multi-tasking Fundamentals
    •  There are two distinct types of multi-tasking:
      • Process Based
      • Thread Based
  • Process Based
    • A Process is a unit of Isolation on a single machine. Processes are fully isolated from each other;
    • Multiple Processes do have to share access to the processing cores, but do not share virtual memory address space and can run in different security contexts.
    • This is the model that web servers have used in the past to process multiple requests.
  • Thread Based
    • Threads are independently schedulable sets of instructions with a package of nonshared resources.
    • A thread is bounded within a process.
    • A thread cannot migrate from one process to another.
    • All threads within a process share process-wide resources such as heap memory and operating system resources such as file handles and sockets.
    • The queue-based approach is also applicable to multiple threads, as it is in fact a general purpose asynchronous pattern.
    • Due to the heavy sharing of resources, using multiple threads benefits a lot from this approach.
    • Resource sharing also introduces less complexity in coordinating multiple worker threads and handling thread failure.
  • Why Multiple-Theads preferred over Multiple-Processes
    •  Windows processes are relatively heavyweight constructs when compared with threads.
    • This is due to the loading of the Win32 runtime libraries and the associated registry reads (along with a number of cross-process calls to system components for housekeeping).
    • Therefore, by design on Windows, we tend to prefer using multiple threads to create asynchronous processing rather than multiple processes.
  • Thread Scheduling
    • In Windows, the operating system component responsible for mapping thread execution on to cores is called the Thread Scheduler.
    • As we shall see, sometimes threads are waiting for some event to occur before they can perform any work (in .NET this state is known as SleepWaitJoin).
    • Any thread not in the SleepWaitJoin state should be allocated some time on a processing core and, all things being equal, the thread scheduler will round-robin processor time among all of the threads currently running across all of the processes.
    • Each thread is allotted a time slice and, as long as the thread doesn’t enter the SleepWaitJoin state, it will run until the end of its time slice.
    • Things, however, are not often equal.
    • Different processes can run with different priorities (there are six priorities ranging from idle to real time).
    • Within a process a thread also has a priority; there are seven ranging from idle to time critical.
    • The resulting priority a thread runs with is a combination of these two priorities, and this effective priority is critical to thread scheduling.
    • if a higher-priority thread wants to run, then a lower-priority thread is ejected from the processor (preempted) and replaced with the higher-priority thread.
    • Threads of equal priority are, again, scheduled on a round-robin basis, each being allotted a time slice.
    • You may be thinking that lower-priority threads could be starved of processor time. However, in certain conditions, the priority of a thread will be boosted temporarily to try to ensure that it gets a chance to run on the processor. Priority boosting can happen for a number of reasons (e.g., user input). Once a boosted thread has had processor time, its priority gets degraded until it reaches its normal value.
  • Threads and Resources
    •  Although two threads share some resources within a process, they also have resources that are specific to themselves.
    • Thread-Specific Resources
      • The Stack
      • Each thread gets its own stack. This means that local variables and parameters in methods, which are stored on the stack, are never shared between threads. The default stack size is 1MB, so a thread consumes a considerable amount of resource in just its allocated stack.
      • Thread Local Storage
      • All threads of a process share its virtual address space.
      • The local variables of a function are unique to each thread that runs the function. However, the static and global variables are shared by all threads in the process.
      • With thread local storage (TLS), you can provide unique data for each thread that the process can access using a global index. 
      • This value is specific to the thread and cannot be accessed by other threads. 
      • TLS slots are limited in number, The constant TLS_MINIMUM_AVAILABLE defines the minimum number of TLS indexes available in each process. which at the time of writing is guaranteed to be at least 64 per process but may be as high as 1,088.
      • Registers
      • A thread has its own copy of the register values. 
      • When a thread is scheduled on a processing core, its copy of the register value is restored on to the core’s registers. This allows the thread to continue processing at the point when it was preempted (its instruction pointer is restored) with the register state identical to when it was last running.
      •  

LINQ in C# - Non-Deferred Operator Examples - Aggregating

Count

Count - Type 1 Example 1

         string[] names = {"Arun", "Raja", "Ramkumar", "RaviRajan" };

                    int Count = names.Count(); // 4
        

Count - Type 2 with Condition Example 1

string[] names = {"Arun", "Raja", "Ramkumar", "RaviRajan" };

                        int Count = names.Count(x=> x.Length==4); // 2

LongCount

The LongCount operator returns the number of elements in the input sequence as a long.
string[] names = {"Arun", "Raja", "Ramkumar", "RaviRajan" };

                        long Count = names.LongCount(x=> x.Length==4); // 2

Sum

The Sum operator cannot have conditions
nt[] names = {1, 2, 3 , 4};

                        int sum= names.Sum()// 10

Min/Max

The Min/Max operator cannot have conditions

Min/Max - Type 1 - Example


                         int[] numbers = {1, 2, 3 , 4};
                        string[] names = { "Person A","Person B", "Person C", "Person D" };     

                        string minString = names.Min(); // Person A
                        Console.WriteLine(minString);
                        int minInt = numbers.Min(); // 1
                        Console.WriteLine(minInt);

Min/Max - Type 2 with Property Selector- Example

List<Customer> Customers = new List<Customer> {
                                    new Customer(){ CustomerID=1, Name="XYZ Private Limited", CustomerType="WholeSale" },
                                    new Customer(){ CustomerID=2, Name="ABC Private Limited", CustomerType="WholeSale" },
                                    new Customer(){ CustomerID=2, Name="MS Infotech", CustomerType="Retail" },
                                    new Customer(){ CustomerID=4, Name="Oracle" , CustomerType="WholeSale" },
                                    new Customer(){ CustomerID=5, Name="IBM Hardware Limited", CustomerType="Retail" },
                                 };

                        string MinCustomerName = Customers.Min(x=> x.Name);
                        Console.WriteLine(MinCustomerName);
//Output
ABC Private Limited

Average

The Average operator returns the average of numeric values contained in the elements of the input sequence.

Average - Type 1 - Example 1

Average returns double return type
 
int[] numbers = {1, 2, 3 , 4};
                        double Avg = numbers.Average();
                        Console.WriteLine(Avg);  // 2.5

Average - Type 2 with Property Selector- Example

class Order
    {
        public int OrderID { get; set; }
        public int CustomerID { get; set; }
        public DateTime OrderDate { get; set; }
        public decimal Total { get; set; }     
    }

List<Order> Orders = new List<Order> {
                                    new Order(){ OrderID=1, OrderDate=new DateTime(2020,10,1), CustomerID=1, Total=1025.24M },
                                    new Order(){ OrderID=2, OrderDate=new DateTime(2020,4,1), CustomerID=1, Total=1000.24M },
                                    new Order(){ OrderID=3, OrderDate=new DateTime(2020,4,1), CustomerID=2, Total=1140.24M },
                                    new Order(){ OrderID=4, OrderDate=new DateTime(2020,2,1), CustomerID=2, Total=1160.24M },
                                    new Order(){ OrderID=5, OrderDate=new DateTime(2020,1,1), CustomerID=3, Total=1567.24M },
                                 };

decimal OrderAverage = Orders.Average(x=> x.Total);
                        Console.WriteLine(OrderAverage);

Aggregate

The easiest-to-understand definition of Aggregate is that it performs an operation on each element of the list taking into account the operations that have gone before.

Aggregate - Type 1 Example 1 Summing numbers

int[] numbers = {1, 2, 3 , 4};                   
                        var sum = numbers.Aggregate((a, b) => a + b); //output: 10 (1 + 2 + 3 + 4)
                        Console.WriteLine(sum);
This adds 1 and 2 to make 3. Then adds 3 (result of previous) and 3 (next element in sequence) to make 6. Then adds 6 and 4 to make 10.

Aggregate - Type 1 Example 2 create a csv from an array of strings

var chars = new[] { "a", "b", "c", "d" };
            var csv = chars.Aggregate((a, b) => a + ',' + b);
            Console.WriteLine(csv); // Output a,b,c,d

Aggregate - Type 2 Example 1 Summing numbers with a seed

int[] numbers = {1, 2, 3 , 4};                   
                        var sum = numbers.Aggregate(10, (a, b) => a + b); //output: 20 = (10 + 1 + 2 + 3 + 4)
                        Console.WriteLine(sum);

Aggregate - Type 2 Example 2 with a seed

var chars = new[] { "a", "b", "c", "d" };
            var csv = chars.Aggregate(new StringBuilder(), (a, b) => {
                if (a.Length > 0)
                    a.Append(",");
                a.Append(b);
                return a;
            });
            Console.WriteLine(csv);
//Output
a,b,c,d

LINQ in C# - Non-Deferred Operator Examples- Any/All/Contains - Quntifiers

Any

The Any operator returns true if any one element of an input sequence matches a condition.

Any - Type 1 - without condition - Example

  • Any without conditions, but a source sequence with at least with a single element. will return true
  string[] names = {"Name1", "Name2", "Name3", "Name4" };

            bool found = names.Any();

            Console.WriteLine(found);
//Output
True
  • Any without conditions, with a source sequence with zero element will return false.
string[] names = {};

            bool found = names.Any();

            Console.WriteLine(found);
 //Output
false

Any - Type 2 - with condition - Example

string[] names = {"Ramkumar", "Rajesh", "Manikandan", "Ram" };

            bool ThreeLetterNamefound = names.Any(x=> x.Length==3);
            Console.WriteLine(ThreeLetterNamefound); //True

            bool FourLetterNamefound = names.Any(x => x.Length == 4);
            Console.WriteLine(FourLetterNamefound); //False

All

The All operator returns true if every element in the input sequence matches a condition.
All needs to have at least one condition, it is mandatory 

string[] names = {"Arun", "Raja" };

            bool ThreeLetterNamefound = names.All(x=> x.Length == 4);
            Console.WriteLine(ThreeLetterNamefound); //True
 //Output
True 

Contains

The Contains operator returns true if any element in the input sequence matches the specified value.

Contains - Type 1 - Example 1

string[] names = {"Arun", "Raja", "Ramkumar", "RaviRajan" };

            bool Namefound = names.Contains("Raja");
            Console.WriteLine(Namefound); //True

Contains - Type 2 with Custom Comparer - Example 1

Implementation of IEqualityComparer, just for shown here for show how IEqualityComparer works


class StringComparer : IEqualityComparer<string>
    {
        public bool Equals(string x, string y)
        {
            //throw new NotImplementedException();
            return x.Length == y.Length;
        }

        public int GetHashCode(string obj)
        {
            //throw new NotImplementedException();
            return obj.GetHashCode();
        }
    }

 string[] names = {"Arun", "Raja", "Ramkumar", "RaviRajan" };

            bool Namefound = names.Contains("abcd", new StringComparer());
            Console.WriteLine(Namefound); //True

            bool NamefoundFalse = names.Contains("abc", new StringComparer());
            Console.WriteLine(Namefound); //False becuase there is no element with 3 letters

LINQ in C# - Non-Deferred Operator Examples- First vs Single/SingleDefault - ElementAt/ElementAtOrDefault- Equality

Single

  • Both First() and Single() will return single element from the source sequence.
  • First() requires at least 1 element, that can be any element from the source squence
  • But Single()  has a special requirements for the sequence
    • If Single() call without a lambda expression as input then the sequence should have only one element 
int[] numbers = {5};

            int found = numbers.Single();
            Console.WriteLine(found);
//Output
5
    • If Single() call with a lambda expression as input then the sequence should have only one matching element for the given lambda expression
int[] numbers = {1, 2, 3};

            int found = numbers.Single(x=> x == 2);
            Console.WriteLine(found);
    • if both the cases doesn't met, Exception will be raised 

SingleOrDefault

  • Works same as FirstDefault

Element / ElementAtDefault

  • Looks for the element at a particular index
string[] names = {"Name1", "Name2", "Name3", "Name4" };

            string found = names.ElementAt(2);

            Console.WriteLine(found);
//Output
Name3
string[] names = {"Name1", "Name2", "Name3", "Name4" };

            string found = names.ElementAtOrDefault(5);
            Console.WriteLine(found);

LINQ in C# - Non-Deferred Operator Examples- (First, FirstOrDefault, Last, LastOrDefault) - Equality

First/Last - Type 1 Example 1

  • First will raise error(System.InvalidOperationException: 'Sequence contains no matching element'),  if cannot find element in the first position that is no element in the squence
string[] presidents = { "Adams", "Arthur", "Buchanan", "Bush", "Carter",
                                    "Cleveland", "Clinton", "Coolidge", "Eisenhower",
                                    "Fillmore", "Ford", "Garfield", "Grant", "Harding",
                                    "Harrison", "Hayes", "Hoover", "Jackson", "Jefferson",
                                    "Johnson", "Kennedy", "Lincoln", "Madison", "McKinley",
                                    "Monroe", "Nixon", "Obama", "Pierce", "Polk", "Reagan",
                                    "Roosevelt", "Taft", "Taylor", "Truman", "Tyler", "Van Buren",
                                    "Washington", "Wilson" };

            Console.WriteLine(presidents.First());
//Output
Adams

First/Last - Type 2 Example 1

 Console.WriteLine(presidents.First(x=> x.StartsWith("H")));

//Output
Harding

FirstOrDefault/LastOrDefault - Example 1

  • Return first element in the sequence if not found return default value of the return type
string foundName = presidents.Take(0).FirstOrDefault();
Console.WriteLine(foundName);


string found = presidents.FirstOrDefault(x => x.Length>20);
            Console.WriteLine(found);

LINQ in C# - Non-Deferred Operator Examples- SequenceEqual - Equality

SequenceEqual

  • The SequenceEqual operator determines whether two input sequences are equal.

Type 1 - Example 1

 List<int> ItemSeq1 = new List<int> { 1, 2, 3 };
 List<int> ItemSeq2 = new List<int> { 1, 2, 3 };

            Console.WriteLine(ItemSeq1.Equals(ItemSeq2));
            Console.WriteLine(ItemSeq1.Equals(ItemSeq1));
//Output
false
true
  • How SequenceEqual it works?
  • SequenceEqual returns true 
    • If the two source sequences are of equal length and their corresponding elements are equal according to the default equality comparer for their type

Type 1 - Example 2

class Pet
{
public string Name { get; set; }
public int Age { get; set; }
}

public static void SequenceEqualEx1()
{
Pet pet1 = new Pet { Name = "Turbo", Age = 2 };
Pet pet2 = new Pet { Name = "Peanut", Age = 8 };
Pet pet3 = new Pet { Name = "Turbo", Age = 2 };
Pet pet4 = new Pet { Name = "Peanut", Age = 8 };

// Create two lists of pets.
List<Pet> pets1 = new List<Pet> { pet1, pet2 };
List<Pet> pets2 = new List<Pet> { pet1, pet2 };
List<Pet> pets3 = new List<Pet> { pet3, pet4 };
List<Pet> pets4 = new List<Pet> { pet3, pet4 };

bool Areequal1 = pets1.SequenceEqual(pets2);
bool Areequal2 = pets3.SequenceEqual(pets4);
bool Areequal3 = pets1.SequenceEqual(pets3);
bool Areequal4 = pets1.SequenceEqual(pets3);

Console.WriteLine("The lists {0} equal.", Areequal1 ? "are" : "are not");
Console.WriteLine("The lists {0} equal.", Areequal2 ? "are" : "are not");
Console.WriteLine("The lists {0} equal.", Areequal3 ? "are" : "are not");
Console.WriteLine("The lists {0} equal.", Areequal4 ? "are" : "are not");

}
 
//Output
The lists are equal.
The lists are equal.
The lists are not equal.
The lists are not equal.

Type 2 - Example 1 using IEquatable Interface

  • If you want to compare the actual data of the objects in the sequences instead of just comparing their references, you have to implement the IEquatable Interface generic interface in your class.
class Pet : IEquatable<Pet>
{
public string Name { get; set; }
public int Age { get; set; }

public bool Equals(Pet other)
{
if (other is null)
return false;

return this.Name == other.Name && this.Age == other.Age; // Compare with Values
}
}


public static void SequenceEqualEx1()
{
Pet pet1 = new Pet { Name = "Turbo", Age = 2 };
Pet pet2 = new Pet { Name = "Peanut", Age = 8 };
Pet pet3 = new Pet { Name = "Turbo", Age = 2 };
Pet pet4 = new Pet { Name = "Peanut", Age = 8 };

// Create two lists of pets.
List<Pet> pets1 = new List<Pet> { pet1, pet2 };
List<Pet> pets2 = new List<Pet> { pet1, pet2 };
List<Pet> pets3 = new List<Pet> { pet3, pet4 };
List<Pet> pets4 = new List<Pet> { pet3, pet4 };

bool Areequal1 = pets1.SequenceEqual(pets2);
bool Areequal2 = pets3.SequenceEqual(pets4);
bool Areequal3 = pets1.SequenceEqual(pets3);
bool Areequal4 = pets1.SequenceEqual(pets3);

Console.WriteLine("The lists {0} equal.", Areequal1 ? "are" : "are not");
Console.WriteLine("The lists {0} equal.", Areequal2 ? "are" : "are not");
Console.WriteLine("The lists {0} equal.", Areequal3 ? "are" : "are not");
Console.WriteLine("The lists {0} equal.", Areequal4 ? "are" : "are not");

}
 
//Output
The lists are equal.
The lists are equal.
The lists are equal.
The lists are equal.

Type 2 - Example 2 using IEqualityComparer Interface

class PetComparer : IEqualityComparer<Pet>
    {
        public bool Equals(Pet x, Pet y)
        {
            if (x is null || y is null)
                return false;

            return x.Name == y.Name && x.Age == y.Age; // Compare with Values
        }

        public int GetHashCode(Pet obj)
        {
            return obj.Name.Length; // Just for returning some integer, there is no logic used here
        }
    }
    class Pet
    {
        public string Name { get; set; }
        public int Age { get; set; }       
    }


            Pet pet1 = new Pet { Name = "Turbo", Age = 2 };
            Pet pet2 = new Pet { Name = "Peanut", Age = 8 };
            Pet pet3 = new Pet { Name = "Turbo", Age = 2 };
            Pet pet4 = new Pet { Name = "Peanut", Age = 8 };

            // Create two lists of pets.
            List<Pet> pets1 = new List<Pet> { pet1, pet2 };
            List<Pet> pets2 = new List<Pet> { pet1, pet2 };
            List<Pet> pets3 = new List<Pet> { pet3, pet4 };
            List<Pet> pets4 = new List<Pet> { pet3, pet4 };

            bool Areequal1 = pets1.SequenceEqual(pets2, new PetComparer());
            bool Areequal2 = pets3.SequenceEqual(pets4, new PetComparer());
            bool Areequal3 = pets1.SequenceEqual(pets3, new PetComparer());
            bool Areequal4 = pets1.SequenceEqual(pets3, new PetComparer());

            Console.WriteLine("The lists {0} equal.", Areequal1 ? "are" : "are not");
            Console.WriteLine("The lists {0} equal.", Areequal2 ? "are" : "are not");
            Console.WriteLine("The lists {0} equal.", Areequal3 ? "are" : "are not");
            Console.WriteLine("The lists {0} equal.", Areequal4 ? "are" : "are not");
//Output
The lists are equal.
The lists are equal.
The lists are equal.
The lists are equal.

LINQ in C# - Non-Deferred Operator Examples - Conversion(ToArray, ToList, ToDictionary, ToLookup)

ToArray and ToList

  • The ToArray operator creates an array of type T from an input sequence of type T.
List<string> StudentNames = new List<string> { "Ram", "Raja", "Ravi"};

      //By default all the query opertors will return either IEnumerable/IQueryable
      IEnumerable<string> StudentsNameIEnumerable = StudentNames.Where(x => x.Length <= 4);

            string[] StudentsNameArray = StudentsNameIEnumerable.ToArray();
            List<string> StudentsNameList = StudentsNameIEnumerable.ToList();

ToDictionary - Type 1 - Example 1

class Customer
    {
        public int CustomerID { get; set; }
        public string CustomerType { get; set; }
        public string Name { get; set; }
    }

List<Customer> Customers = new List<Customer> {
                                    new Customer(){ CustomerID=1, Name="XYZ Private Limited", CustomerType="WholeSale" },
                                    new Customer(){ CustomerID=2, Name="ABC Private Limited", CustomerType="WholeSale" },
                                    new Customer(){ CustomerID=3, Name="MS Infotech", CustomerType="Retail" },
                                    new Customer(){ CustomerID=4, Name="Oracle" , CustomerType="WholeSale" },
                                    new Customer(){ CustomerID=5, Name="IBM Hardware Limited", CustomerType="Retail" },
                                 };

Dictionary<int, Customer> CustomerDictionary = Customers.ToDictionary(x => x.CustomerID);

ToDictionary - Type 2 with Custom comparer - Example 1

  • Same as like Group by Example with Custom Comparer, we can implement IEqualityComparer interface to compare the keys and for dictionary key must be unique, if not System.ArgumentException: 'An item with the same key has already been added.' exception will be thrown
class Customer
    {
        public int CustomerID { get; set; }
        public string CustomerType { get; set; }
        public string Name { get; set; }
    }

 class CustomerEqualityComparer : IEqualityComparer<int>
    {
        public bool Equals(int x, int y)
        {
            if (this.GetHashCode(x) == this.GetHashCode(y))
                return true;
            else
                return false;           
        }

        public int GetHashCode(int obj)
        {
            return obj;
        }
    }

List<Customer> Customers = new List<Customer> {
                                    new Customer(){ CustomerID=1, Name="XYZ Private Limited", CustomerType="WholeSale" },
                                    new Customer(){ CustomerID=2, Name="ABC Private Limited", CustomerType="WholeSale" },
                                    new Customer(){ CustomerID=3, Name="MS Infotech", CustomerType="Retail" },
                                    new Customer(){ CustomerID=4, Name="Oracle" , CustomerType="WholeSale" },
                                    new Customer(){ CustomerID=5, Name="IBM Hardware Limited", CustomerType="Retail" },
                                 };

Dictionary<int, Customer> CustomerDictionary = Customers.ToDictionary(x => x.CustomerID, new CustomerEqualityComparer() );

            foreach (var item in CustomerDictionary)
            {
                Console.WriteLine(item.Key);
                Console.WriteLine(item.Value.Name);

            }
//Output
1
XYZ Private Limited
2
ABC Private Limited
3
MS Infotech
4
Oracle
5
IBM Hardware Limited

ToDictionary - Type 3 with Custom Return Type- Example 1

  • This type is same as the first Type, with the small change that for the value type of the Dictionary, we specify a custom type
Dictionary<int, string> CustomerDictionary = Customers.ToDictionary(x => x.CustomerID, x=> x.Name);

ToDictionary - Type 4 Example 1

  • This type is a combination of Type 2 and Type 3.
Dictionary<int, string> CustomerDictionary = Customers.ToDictionary(x => x.CustomerID, x=> x.Name, new CustomerEqualityComparer());

ToLookup

  • Lookup works as a combination GroupBy and Dictionary
  • i.e. Same as like a Dictionary, it holds elements as a key,value pair, but in Dictionary each key will be with Single Value
  • But Lookup have Single key with Multiple Values, same as like a GroupBy option
  • All the 4 Types we saw above is applicable to ToLookUp duplicate Keys. 
//Type 1

List<Customer> Customers = new List<Customer> {
                                    new Customer(){ CustomerID=1, Name="XYZ Private Limited", CustomerType="WholeSale" },
                                    new Customer(){ CustomerID=2, Name="ABC Private Limited", CustomerType="WholeSale" },
                                    new Customer(){ CustomerID=2, Name="MS Infotech", CustomerType="Retail" },
                                    new Customer(){ CustomerID=4, Name="Oracle" , CustomerType="WholeSale" },
                                    new Customer(){ CustomerID=5, Name="IBM Hardware Limited", CustomerType="Retail" },
                                 };

ILookup<int, Customer> CustomerDictionary = Customers.ToLookup(x => x.CustomerID, new CustomerEqualityComparer());
           
            foreach (var item in CustomerDictionary)
            {
                Console.WriteLine(item.Key);

                foreach (var itemdetail in item)
                {
                    Console.WriteLine(itemdetail.Name);
                }
            }
//Output
1
XYZ Private Limited
2
ABC Private Limited
MS Infotech

4
Oracle
5
IBM Hardware Limited

//Type 2

ILookup<int, string> CustomerDictionary = Customers.ToLookup(x => x.CustomerID, x => x.Name);
           
            foreach (var item in CustomerDictionary)
            {
                Console.WriteLine(item.Key);

                foreach (var itemdetail in item)
                {
                    Console.WriteLine(itemdetail); //Customer Name
                }
            }

//Type 3

ILookup<int, string> CustomerDictionary = Customers.ToLookup(x => x.CustomerID, new CustomerEqualityComparer());
           
            foreach (var item in CustomerDictionary)
            {
                Console.WriteLine(item.Key);

                foreach (var itemdetail in item)
                {
                   Console.WriteLine(itemdetail.Name);
                }
            }

//Type 4

ILookup<int, string> CustomerDictionary = Customers.ToLookup(x => x.CustomerID, x=> x.Name, new CustomerEqualityComparer());
           
            foreach (var item in CustomerDictionary)
            {
                Console.WriteLine(item.Key);

                foreach (var itemdetail in item)
                {
                    Console.WriteLine(itemdetail); //Customer Name
                }
            }

LINQ in C# - Examples - Range, Repeat, Empty - Generation

Range - Example

IEnumerable<int> ints = Enumerable.Range(1, 5);

            foreach (var item in ints)
                Console.WriteLine(item);
 //Output
1
2
3
4
5

Repeat - Example

IEnumerable<int> ints = Enumerable.Repea(7, 5);

            foreach (var item in ints)
                Console.WriteLine(item);
 //Output
7
7
7
7
7

Empty- Example

  • The Empty operator generates an empty sequence of a specified type.
IEnumerable<string> strings= Enumerable.Empty<string>();         
                Console.WriteLine(strings.count());
 //Output
0

LINQ in C# - Examples - Conversion Operators (Cast, OfType) and AsEnumerable, DefaultIfEmpty

  • The conversion operators provide a simple and convenient way of converting sequences to other collection types.

Cast

  • The Cast operator is used to cast every element of an input sequence to an output sequence of the specified type. 
  • Fundamentally, Cast() is implemented like this:
public IEnumerable<T> Cast<T>(this IEnumerable source)
{
foreach(object o in source)
yield return (T) o;
}
  • As you can see from the implementation of Cast(), first in cast every element of the sequence as object o, as object as the base type of all the types in C#, then it simply try to cast the object type to corresponding type using explicit casting (T) o, if this casting fails it will throw InvalidCastException.
  • So while casting of non-generic collection holding same type of object, then only we can use Cast() operator
  • Example 1
object[] ItemsSet1 = new object[]{ "01", "02", "03"};

            string[] AfterCasting = ItemsSet1.Cast<string>().ToArray();
           
            foreach (var item in AfterCasting)
                Console.WriteLine(item);
//Output
01
02
03
  • Example 2
 ArrayList ItemsSet1 = new ArrayList{ 4, 3, 2, 1};

            List<int> AfterCasting = ItemsSet1.Cast<int>().ToList();
           
            foreach (var item in AfterCasting)
                Console.WriteLine(item);
//Output
4
3
2
1

OfType

  • If a non-generic collection contains different types of objects and we want to extract only one type of object from the collection then we can use OfType.
  • For understanding how OfType works, refer the implementation below.
public IEnumerable<T> OfType<T>(this IEnumerable source)
{
foreach(object o in source)
if(o is T)
yield return (T) o;
}
  • Example 1
ArrayList ItemsSet1 = new ArrayList{ "Raja", 3, "Ravi", 1};

            List<int> AfterCasting = ItemsSet1.OfType<int>().ToList();
           
            foreach (var item in AfterCasting)
                Console.WriteLine(item);
//Output
3
1

AsEnumerable

  • The AsEnumerable operator simply causes its input sequence of type IEnumerable to be returned as type IEnumerable
  • Example - this example is using Query Expression Syntax
Northwind db = new Northwind(@"Data Source=.\SQLEXPRESS;Initial Catalog=Northwind");

var custs = from c in db.Customers
                  where c.City == "Rio de Janeiro"
                  select c;
foreach (var cust in custs)
           Console.WriteLine("{0}", cust.CompanyName);

DefaultIfEmpty

  • Refer the example below
List<string> StudentNames = new List<string> { "Ram", "Raja", "Ravi"};

            //In below statement the assumption is that, there is atleast one name with length >5
            //so the method first will return the first name with length >5
            //But there is no name, so exception will be thrown at runtime of Type System.InvalidOperationException:

            string FoundName = StudentNames.Where(x => x.Length > 5).DefaultIfEmpty().First();
            if(FoundName!=null)
                Console.WriteLine("found Name {0}", FoundName);
            else
                Console.WriteLine("No name found");
  • Fix is using DefaultIfEmpty Example 1 - returns default value of type string is null
List<string> StudentNames = new List<string> { "Ram", "Raja", "Ravi"};           

string FoundName = StudentNames.Where(x => x.Length > 5).DefaultIfEmpty().First();

            if(FoundName!=null)
                Console.WriteLine("found Name {0}", FoundName);
            else
                Console.WriteLine("No name found");

//Output
No name found
  • Fix is using DefaultIfEmpty Example 2 with Default Specified
 List<string> StudentNames = new List<string> { "Ram", "Raja", "Ravi"};
           
 string FoundName = StudentNames.Where(x => x.Length > 5).
DefaultIfEmpty("Akshara").First();
     
            Console.WriteLine("found Name {0}", FoundName);
//Output
found Name Akshara

LINQ in C# - Examples - Set Operators (Distinct, Union, Intersect)

  • The set operators are used to perform mathematical set-type operations on sequences.

Distinct - Example

string[] PersonNames = { "Adams", "Arthur", "Buchanan", "Bush", "Carter", "Cleveland",
"Roosevelt","Coolidge", "Eisenhower", "Fillmore", "Ford", "Garfield",
"Grant", "Harding","Harrison", "Hayes", "Hoover", "Jackson", "Jefferson", "Johnson", "Kennedy","Lincoln", "Madison", "McKinley", "Monroe", "Nixon", "Fillmore", "Pierce", "Polk","Reagan", "Roosevelt", "Fillmore", "Taylor", "Truman", "Tyler", "Van Buren", "Washington",
                                    "Wilson" };


            Console.WriteLine($"No of Persons with Duplicates: {PersonNames.Count()}");

            string[] NoDuplicates = PersonNames.Distinct().ToArray();

            Console.WriteLine($"No of Persons with out Duplicate: {NoDuplicates.Count()}");
//Output
No of Persons with Duplicates: 38
No of Persons with out Duplicate: 35

Union- Example

  • Union and Concat do the same, But if there is duplicate entry Union will remove it and Concat will not remove duplicate elements.
 string[] ItemsSet1 = { "Item1", "Item2", "Item3"};
            string[] ItemsSet2 = { "Item1", "Item4", "Item5" };

            IEnumerable<string> Concatenated = ItemsSet1.Concat(ItemsSet2);
            IEnumerable<string> Unioned = ItemsSet1.Union(ItemsSet2);

            Console.WriteLine($"No of Item After Concat: {Concatenated.Count()}");
            Console.WriteLine($"No of Item After Union: {Unioned.Count()}");
//Output
No of Item After Concat: 6
No of Item After Union: 5

Intersect- Example

string[] ItemsSet1 = { "Item1", "Item2", "Item3"};
            string[] ItemsSet2 = { "Item1", "Item4", "Item5" };
          
            IEnumerable<string> CommonItems = ItemsSet1.Intersect(ItemsSet2);

            Console.WriteLine($"No of Common Item Between these sets: {CommonItems.Count()}");
            foreach (var item in CommonItems)
                Console.WriteLine(item);
//Output
No of Common Item Between these sets: 1
Item1

Except- Example 

  • Minus off the second sequence elements from the first sequence
string[] ItemsSet1 = { "Item1", "Item2", "Item3"};
            string[] ItemsSet2 = { "Item1", "Item4", "Item5" };
          
            IEnumerable<string> AfterMinusing = ItemsSet1.Except(ItemsSet2);

            Console.WriteLine($"Item sets1 -(minus) Items sets2: {AfterMinusing.Count()}");
            foreach (var item in AfterMinusing)
                Console.WriteLine(item)
//Output
Item sets1 -(minus) Items sets2: 2
Item2
Item3

LINQ in C# - Examples - GroupBy - Grouping

  • The grouping operators assist with grouping elements of a sequence together by a common key.
  • GroupBy operator return a sequence of IGrouping<K, T> elements.
    • Where K - is key of field type which is used for grouping
    • Where T -  is actual object

GroupBy- Type 1 - with default comparer- Example

class Customer
    {
        public int CustomerID { get; set; }
        public string CustomerType { get; set; }
        public string Name { get; set; }
    }
class Program
    {       
        static void Main(string[] args)
        {
            List<Customer> Customers = new List<Customer> {
                                    new Customer(){ CustomerID=1, Name="XYZ Private Limited", CustomerType="WholeSale" },
                                    new Customer(){ CustomerID=2, Name="ABC Private Limited", CustomerType="WholeSale" },
                                    new Customer(){ CustomerID=3, Name="MS Infotech", CustomerType="Retail" },
                                    new Customer(){ CustomerID=4, Name="Oracle" , CustomerType="WholeSale" },
                                    new Customer(){ CustomerID=5, Name="IBM Hardware Limited", CustomerType="Retail" },
                                 };

         

            IEnumerable<IGrouping<string, Customer>> CustomersGroup = Customers.GroupBy(x => x.CustomerType);
           
            foreach (var item in CustomersGroup)
            {
                Console.WriteLine($"Group Name:{item.Key}\n");
                
                Console.WriteLine("Customers");
                Console.WriteLine("-------------");
                foreach(Customer C in item)
                {
                    Console.WriteLine($"ID:{C.CustomerID}, Name:{C.Name}");
                }
                Console.WriteLine();
            }
            Console.ReadLine();
        }                              
           
    }
//Output
Group Name:WholeSale

Customers
-------------
ID:1, Name:XYZ Private Limited
ID:2, Name:ABC Private Limited
ID:4, Name:Oracle

Group Name:Retail

Customers
-------------
ID:3, Name:MS Infotech
ID:5, Name:IBM Hardware Limited
  • As you can see from the above example GroupBy returns IGrouping<string, Customer>,in this key of type string because Customer.CustomerType is of type string and the as source type is of type Customer
  • It is not compulsory to put return type as  IEnumerable<IGrouping<string, Customer>>, it can be declared as var or can be return as a List, just for understanding I have used IEnumerable<IGrouping<string, Customer>>
List<IGrouping<string, Customer>> CustomersGroup = Customers.GroupBy(x => x.CustomerType).ToList();
            foreach (var item in CustomersGroup)
            {
                Console.WriteLine($"Group Name:{item.Key}\n");
               
                Console.WriteLine("Customers");
                Console.WriteLine("-------------");
                foreach(Customer C in item)
                {
                    Console.WriteLine($"ID:{C.CustomerID}, Name:{C.Name}");
                }
                Console.WriteLine();
            }
            Console.ReadLine();
  • Both(above/below) versions will return the same result as first Example
var CustomersGroup = Customers.GroupBy(x => x.CustomerType)
            foreach (var item in CustomersGroup)
            {
                Console.WriteLine($"Group Name:{item.Key}\n");
               
                Console.WriteLine("Customers");
                Console.WriteLine("-------------");
                foreach(Customer C in item)
                {
                    Console.WriteLine($"ID:{C.CustomerID}, Name:{C.Name}");
                }
                Console.WriteLine();
            }
            Console.ReadLine();

GroupBy- Type 2 - with Custom comparer- Example

  • IEqualityComparer<T> Interface allows the implementation of customized equality comparison for collections
  • That is, you can create your own definition of equality for type T, and specify that this definition be used with a collection type that accepts the IEqualityComparer<T> generic interface.
  • Before we look into GroupBy - Type 2, we will discuss about IEqualityComparer interface and how it works
class CustomerEqualityComparer : IEqualityComparer<string>
    {
        public bool Equals(string x, string y)
        {
            if (this.GetHashCode(x) == this.GetHashCode(y))
                return true;
            else
                return false;          
        }

        public int GetHashCode(string obj)
        {
            return obj.Length; // Just any logic to generate hashcode
        }
    }
  • As you can see from above code the IEqualityComparer contains two methods GetHashCode and Equals
    •  GetHashCode- method is just a helper method, as IEqualityComparer<T> generic interface, we are passing string type here as CustomerType is of type string.
    • It is upto us to decide how the hashcode will be generator, 
    • Even without calling this method also we can 
    • Equals - method is the actual method for checking the equality, it is up to us to decide whether we are going to use the GetHashCode or not, for checking the equality.
    • Just for to show how it works I have implemented a dummy logic.

GroupBy- Type 2 - with Custom Equality comparer- Example

IEnumerable<IGrouping<string, Customer>> CustomersGroup = Customers.GroupBy(x => x.CustomerType, new CustomerEqualityComparer());
          
            foreach (var item in CustomersGroup)
            {
                Console.WriteLine($"Group Name:{item.Key}\n");
               
                Console.WriteLine("Customers");
                Console.WriteLine("-------------");
                foreach(Customer C in item)
                {
                    Console.WriteLine($"ID:{C.CustomerID}, Name:{C.Name}");
                }
                Console.WriteLine();
            }
            Console.ReadLine();
  • Any way the result will be the same as the first type, as we didn't do any big change in the Custom Comparer class logic

GroupBy- Type 3 - Example

  • This type is same as the first Type, with the small change the return type of the GroupBy operator
  • In the Type 1 Example, we just specify the group by column, so the return type is the base type of the sequence i.e Customer by default
  • We have a option to specify the return data/type of the object as custom
  • In this example i am going to change the first type slightly and show to how to specify the custom return type.
IEnumerable<IGrouping<string, Customer>> CustomersGroup = Customers.GroupBy(x => x.CustomerType, y => y);
            foreach (var item in CustomersGroup)
            {
                Console.WriteLine($"Group Name:{item.Key}\n");
               
                Console.WriteLine("Customers");
                Console.WriteLine("-------------");
                foreach(Customer C in item)
                {
                    Console.WriteLine($"ID:{C.CustomerID}, Name:{C.Name}");
                }
                Console.WriteLine();
            }
            Console.ReadLine();
IEnumerable<IGrouping<string, string>> CustomersGroup = Customers.GroupBy(x => x.CustomerType, y => y.Name);
            foreach (var item in CustomersGroup)
            {
                Console.WriteLine($"Group Name:{item.Key}\n");
               
                Console.WriteLine("Customers");
                Console.WriteLine("-------------");
                foreach(string CustomerName in item)
                {
                    Console.WriteLine($"Name:{CustomerName}");
                }
                Console.WriteLine();
            }
            Console.ReadLine();

GroupBy- Type 4 - Example

  • Type 4 is nothing but a combination of both Type 2 and Type 3
IEnumerable<IGrouping<string, string>> CustomersGroup = Customers.GroupBy(x => x.CustomerType, y => y.Name, new CustomerEqualityComparer());
            foreach (var item in CustomersGroup)
            {
                Console.WriteLine($"Group Name:{item.Key}\n");
               
                Console.WriteLine("Customers");
                Console.WriteLine("-------------");
                foreach(string CustomerName in item)
                {
                    Console.WriteLine($"Name:{CustomerName}");
                }
                Console.WriteLine();
            }
            Console.ReadLine();

Framework Fundamentals - String - Comparing Strings

In comparing two values, the .NET Framework differentiates the concepts of equality comparison and order comparison . Equality compariso...