Generic Delegates (Action, Func & Predicates) in C#

Generic Delegates (Action, Func & Predicates) in C#

In C#, delegates are used to refer to methods with a specific signature. Generic delegates extend this concept by allowing you to create delegates that can work with any method signature, rather than being tied to a specific signature.

Here’s a basic example of a generic delegate:

delegate T MyGenericDelegate<T>(T arg);

You can use this generic delegate as follows.

MyGenericDelegate<int> handler1 = CustomSquare;
int square=handler1(4);
Console.WriteLine(square); //16

MyGenericDelegate<string\> handler2 = CustomToUpper;
Console.WriteLine(CustomToUpper("helLo")); //HELLO

In-built Generic delegates

C# comes with inbuilt generic delegates like Action, Func and Predicate.

[Read More]

Delegates, Anonymous Method and Lambda Expression

Delegates, Anonymous Method and Lambda Expression
  • Delegates in C# are essentially type-safe function pointers.
  • They allow you to treat methods as objects, enabling you to pass methods as parameters, store them in variables, and invoke them dynamically.
  • Delegates are particularly useful for implementing callback mechanisms and event handling in C#.

delegate void MyDelegate(string message);

You can point this delegate to any method with similar signature.

namespace BasicsOfCSharp;

// Declare a delegate type
delegate void MyDelegate(string message);

class Program
{
 // Method that matches the delegate signature
 static void DisplayMessage(string message)
 {
 Console.WriteLine("Message: " + message);
 }
 static void Main()
 {
        // Instantiate the delegate.
        MyDelegate handler = DisplayMessage;

        // calling the handler
        handler("Hello..");

    }

}

👉Multicast Delegate:

[Read More]

Abstract Class in C#

Abstract Class in C#

In c#, Abstract class is a class that can not be instantiated on its own

  • In c#, Abstract class is a class that can not be instantiated on its own
  • It is typically used as a base class for other class.
  • Abstract class provides a way to achieve abstraction, because there you can just declare the methods (abstract methods) and implement them later.
  • It can contain both abstract methods (methods without implementation details) and non-abstract methods (method with implementation details).
  • Similar goal can be achieved with interface, but in abstract class you can also define non-abstract method. These methods are needed when you need to share some common functionality.
public abstract class Shape
{
 // it is the basic syntax of abstract class
}

Example:

[Read More]

Polymorphism in depth with C#

Polymorphism in depth with C#

Polymorphism is one of the core concept of object oriented programming. Word polymorphism = poly (many) + morphism (forms). As its name suggesting polymorphism means, an entity can have multiple forms.

📢 Updated and refinded at : 21-feb-2025

Other oops core concepts :

Let’s learn polymorphism through mistakes.

Jim requires an entry system for his pet shop, which exclusively houses various breeds of dogs. This system will manage the entry and records of all dogs entering and exiting the premises.

[Read More]

Inheritance in C#

Inheritance in C#

📢 Updated and refinded at : 21-feb-2025

Inheritance is a fundamental concept in OOPs that allows a class to inherit properties and behaviors of another class. We have two key terms are here base class/superclass and derived class/subclass.

  • Base class / super-class: Whose members and functionality are inherited (Giver).
  • Derived class / sub-class: Who is inheriting the members (Taker)

📺Other oops concepts:

Syntax of inheritance:

[Read More]

Encapsulation in C#

Encapsulation in C#

📢 Updated and refinded at : 21-feb-2025

Bundling the data member and member function into a single unit is called encapsulation. Remember the term “capsule”. We put all the medicine inside a wrapper and call it capsule. Similarly, wrap the data members and member functions together is called encapsulation.

Now we need to understand few terms.

  • Data members : Attributes or properties (eg. name, age)
  • Member functions: Methods (PrintDetails)

👉 We wrap up the data members (attributes or properties) and member functions (methods) into a single unit (class).

[Read More]

Abstraction in C#

Abstraction in C#

📢 Updated and refinded at : 21-feb-2025

Abstraction allows you to focus on the relevant details of an object while ignoring unnecessary complexities.
This is achieved by defining a simplified representation of an object that hides the implementation details and exposes only the necessary features.
In practical terms, abstraction is implemented through abstract classes and interfaces in languages like C#.

📺Other OOPs related articles :

[Read More]

Fluent Validation in Dotnet Core

Fluent Validation in Dotnet Core

Fluent validation

Fluent validation is an open source library for validating the models, which is free as of I am writing this article.

📢 📝 Last Updated: 25-March-2025

Why fluent validation?

If you already have used data annotation for validation, then you must be aware of the validation in .NET. So you might be thinking why do we need a fluent validation then.

Fluent validation helps you to separate validation logic from your models. That makes your code clean. If you have complex validation logic, then you want to define it separately rather than making your model unreadable.

[Read More]

Uploading Images in Blazor Server

Uploading Images in Blazor Server

In this blog post we are going to learn how to upload files in the blazor server application. I am going to upload images in this tutorial but you can upload any file (i have created reusable code).

💻Source Code: https://github.com/rd003/BlazorFile/

High level overview

We will upload images to a folder of a local machine ( on a production you have to use cloud storage) with a unique name (e.g. sd$3abccc3$1.png ), that name is going to save in database.

[Read More]

Easiest Way to Handle Csv Files in Csharp

Easiest Way to Handle Csv Files in Csharp

In this tutorial we will se how to read and write data to csv file. It is pretty much easy if you have some external library for that. We are definitely going to use a library and that will be CsvHelper.

You can also check the video version of this tutorial.

First and foremost, create a c# console application in .net core. After that we need to install a nuget package, which is**CsvHelper**

[Read More]