Skip to main content

                                                                     Delegates in C#


You want to pass a function as a Parameter ? How does C# handles a callback function or an event handler ? The answer of all these questions is one i.e. a Delegate.

Delegate is a reference type datatype that defines a method prototype. We can define a varibale of delegate just like any other datatype that can refer to any method with the same prototype i,e, signature. 

Define a Delegate => Set a Target Method => Invoke 

eg: public delegate void MyFirstDelegate(string str); // Declaration

Note: declaration can be done outside or even inside the class.

static void MethodA(string str)

{

Console.Writeline(str);

}

//Setting up Target Method

1) MyFirstDelegate del = new MyFirstDelegate(MethodA); 

2) MyFirstDelegate del= MethodA;

3) MyFirstDelegate del = (string msg) => Console.Writeline(msg);


//Invoke 

del.Invoke("Hello World!");

del("Hello World");

Comments

Popular posts from this blog

Abstract Factory Design Pattern

Abstract Factory : return families of related or dependent objects without specifying their concrete classes. AbstractFactory- IVehicle ConcreteFactory - Maruti, Honda AbstractProduct- IDiesel, IPetrol Product- DezirePetrol, ZenDiesel, AmazeDiesel, CityPetrol Client- This is a class which uses AbstractFactory and AbstractProduct interfaces to create a family of related objects Implementation is pretty much straight forward: Since now you have good understanding of Factory, there is nothing much to explain. using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace AbstractFactory {     class Program     {         static void Main(string[] args)         {             IVehicle marutiVehicle = new Maruti();             CarClient marutiClient = new CarClient(marutiVehicle);             Console.WriteLine("********* MARUTI **********");             Console.WriteLine(marutiClient.Get

Database Factory Design Pattern C#

Many developers has confusion on how to write database code in application. I will discuss several approaches here and try to provide explanation on each approach. Approach 1 : Easiest approach which is very straight forward which will work absolutely fine. The issues we will get to manage the project in terms of software principles. Like if you going to change Database provider, you would need to change using statements and other issue like mixing Database code with business logic etc. I will not go in detail here, I am assuming you already have an understanding of design principles. I am just exploring different approaches to achieve the database connectivity. using System.Collections.Generic; using System.Data.SqlClient; namespace DatabaseFactory {     class Program     {         static void Main(string[] args)         {             var employees = SomeDabOperation();         }         private static List<Employee> SomeDabOperation()         {             u