Asynchronous Programming in C# - Comparing it with multi-threading

async and await in c#

Asynchronous task is a non-blocking task. Main thread goes back to the thread pool (and free to do other tasks) when it reaches to await – and new thread is assigned when wait is complete.

It is different from the multi-threading. In multi-threding, task is divided between multiple threads. Cores of your CPU are utilized.

Analogy

Let’s say you have some chores like:

  1. Boiling eggs 🫕 (or sweet potatoes🍠🍠 if you dont eat eggs 🥚🥚)
  2. Clean 🧹🪣 the house

There are multiple ways to achieve this:

[Read More]

C#: Byte and Its Use Cases

c# byte

  • byte is a value type in c#
  • byte is an unsigned integer which stores value from 0 to 255, which means it can not store negetive numbers.
  • Size of byte is 8-bit (1 byte)
  • CTS equivalent of byte is System.Byte
  • It’s default value is 0
  • It is great for memory optimization when dealing with small numbers.

Examples:

byte a = 10;

Console.WriteLine(a);

Use cases

  1. Working with ASCII value
 byte asciiValue = (byte)'R'; // 82
 byte asciiValue2 = (byte)'r'; // 114
  1. Encoding text into bytes
 using System.Text;

 string message = "Ravindra";
 byte[] encoded = Encoding.UTF8.GetBytes(message);

 Console.WriteLine($"{string.Join(",",encoded)}");

 // op: 82,97,118,105,110,100,114,97

Note: Each character is encoded using UTF-8. For standard letters, the byte values match their ASCII codes.. (R: 82, a:97, v:118 …)

[Read More]

IEnumerable Vs IQueryable In C#

IEnumerable-vs-IQuerable

There has been a discussion around town about the difference between an IEnumerable and an IQueryable, especially in c# interviews. I won’t be diving into the fact that IEnumerable is part of the System.Collections namespace and IQueryable belongs to System.Linq namespace (or did I???). Rather, I’ll focus on the practical usage of both—how they work, and when to use each.

IQueryable

 public IActionResult GetPeople()
 {
  // It will retrieve 2 records from database 
  IQueryable<Person> people = _context.People.Take(2); 
  //  Note: At the above line, no data will be retrieved from the database

  return Ok(people); // Data will be retrieved here
 }

Corresponding sql

Note that, I am using Sqlite, the above code is translated to this query:

[Read More]

LINQ: Zip() operator

zip() operator in

There is also a video version of this tutorial.

Let’s understand it with example.

int[] nums1 = [1, 2, 3, 4];
int[] nums2 = [3, 4, 5, 6];

IEnumerable<int>? product = nums1.Zip(nums2, (n1, n2) => n1 * n2);

Console.WriteLine(string.Join(", ", product));  // 3, 8, 15, 24

Let’s break it down:

IEnumerable<int>? product = nums1.Zip(nums2, (n1, n2) => n1 * n2);

It takes nums1[i] and nums2[i], evaluates it (nums1[0]*nums2[0]) and returns it. Here i is the index of the array. For example.

[Read More]

SingleAsync vs SingleOrDefaultAync vs FirstAsync vs FirstOrDefaultAsync vs FindAync

SingleAsync() vs SingleOrDefaultAync() vs FirstAsync() vs FirstOrDefaultAsync() vs FindAync() in c#

I don’t think there is any need of introduction. Let’s jump to the code section. We coders understand with code more. Let’s understand the concept then you don’t need to remember any definition.

This is the recordset against which I am running queries.

IdFirstNameLastName
1JohnDoe
2RavindraDevrani
3MohanSingh
20MaheshSoni
21JohnSnow

First and foremost let’s say we need a record with LastName=”Doe”.

[Read More]

Curious Case of LINQ Group By

curious case of linq group by

Schema

  • Department (DepartmentId, Name)
  • Employee (EmployeeId, Name, DepartmentId)

Result set I need

Show all departments with total number of employees. Do not skip the departments which have 0 employees. As shown below:

DepartmentIdNameTotalEmployees1Engineering22Marketing13HR0

I have applied various queries and checked their equivalent sql.

1. Straightforward but skips the department which has no employees

This query does not meet my requirement. It would be a good choice if I don’t need departments without any employees.

[Read More]

LINQ: SelectMany()

select many operator in linq

NOTE: You can find the source code here.

Schema Overview

// Department
public class Department
{
    public int DepartmentId { get; set; }
    public string Name { get; set; } = string.Empty;

    public ICollection<Employee> Employees { get; set; } = [];
}

// Employee

public class Employee
{
    public int EmployeeId { get; set; }
    public string Name { get; set; } = string.Empty;

    public int DepartmentId { get; set; }

    public Department Department { get; set; } = null!;

    public ICollection<EmployeeSkill> EmployeeSkills { get; set; } = [];
}

// Skills

public class Skill
{
    public int SkillId { get; set; }
    public string Name { get; set; } = string.Empty;

    public ICollection<EmployeeSkill> EmployeeSkills { get; set; } = [];
}

// EmployeeSkills (Junction table)

public class EmployeeSkill
{
    public int EmployeeId { get; set; }
    public int SkillId { get; set; }

    public Employee Employee { get; set; } = null!;
    public Skill Skill { get; set; } = null!;
}

In simpler terms:

[Read More]

Higher Order Functions in C#

Higher-order functions (HOF) are functions that can take a function as an argument or return a function or both. In C#, higher-order functions are achieved using delegates, lambda expressions and expression trees.

Example 1: Using a Delegate

// Declare a delegate that takes an int and returns an int
public delegate int IntOperation(int x);

public class Program
{
    // Higher-order function that takes a delegate as an argument
    public static int PerformOperation(int value, IntOperation operation)
    {
    return operation(value);
    }

    public static void Main()
    {
        // Create a delegate instance that points to the Square method
        IntOperation square = x => x \* x;

        // Pass the delegate to the higher-order function
        int result = PerformOperation(5, square);
        Console.WriteLine(result); // Output: 25
    }

}

Example 2 : Using lambda expression

// Higher-order function that takes a lambda expression as an argument
public static int PerformOperation(int value, Func<int, int\> operation)
{
 return operation(value);
}

// main method

// Define a lambda expression that squares a number
Func<int, int> square = x => x * x;

// Pass the lambda expression to the higher-order function
int result = PerformOperation(5, square);

Example 3: Function as a Return Value

public class Program
{
    // Higher-order function that returns a function
    public static Func<int, int\> GetOperation(bool isSquare)
    {
        if (isSquare)
        {
            return x => x \* x;
        }
        else
        {
            return x => x + x;
        }
    }

    public static void Main()
    {
        // Get a function that squares a number
        Func<int, int> operation = GetOperation(true);

        // Use the returned function
        int result = operation(5);
        Console.WriteLine(result); // Output: 25
    }
}

In c#, higher order functions are everywhere. If one have used LINQ, must have used HO functions. Collection’s Where() is the good example.

[Read More]

Let's dive into various types of properties in c#

various types of properties in c#

I assume you have some knowledge of C# properties. If not, here’s a quick definition: A property is a class member that provides a flexible way to read, write, or compute the value of a private field. Don’t worry if you’re new to properties; we’ll be using them in this blog, and I’ll explain them as we go along.

1. Properties with getter and setter

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

Generally, we define properties like this. The Person class has two properties Name and Age, each with getters and setters. This enables you to set and read their values.

[Read More]

Understanding The Pure And Impure Functions in C#

What is pure function in c#?

Pure functions

Pure function is that:

  1. Always returns the same output given the same inputs.
  2. Does not modify the state of object or any external state.
  3. Does not have any side effects such as I/O operations, exceptions or modifying external variables.
  • In other words, a pure function takes input, process it and returns the output without modifying anything else.
  • Pure functions are easy to test and debug.

Examples

  1. Mathematical calculations
public int Add(int a, int b)
{
 return a + b;
}
  1. String manipulation
public string Concatenate(string a, string b)
{
 return a + b;
}
  1. Array operations
public int[] SortArray(int[] array)
{
 return array.OrderBy(x => x).ToArray();
}
  1. Data transformations
public string ConvertToString(int value)
{
 return value.ToString();
}
  1. Validation
public bool IsValidEmail(string email)
{
 return email.Contains("@");
}

Impure Functions

Impure function is that

[Read More]