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

About Us

Ashish Joshi C# TestDome Certificate Hi, Welcome to my blog on various topics on .Net Stack basically. My name is Ashish Joshi and I’m a software developer. I majorly work with .NET. I recently started writing  blogs about .NET but since now .Net Stack is huge and crossing it's boundaries, it's  compatible with other technology stacks, you’ll see posts from other technologies as well. I hope you’ll find a lot of interesting topics here that you can use in your own assignments. You can find me on LinkedIn I am.planning to write in coming days (2017-2018) on: Popular Interview Questions For Experienced .Net resources  MongoDb in .NET Various Design Patterns Machine Learning - will try to demonstrate how it can be done in .net and in other technology Artificial Intelligence - same as machine learning I'll demonstrate using some running examples Feel free to provide your feedback and suggestions.

Privacy Policy

This blog does not share personal information with third parties nor do we store any information about your visit to this blog other than to analyze and optimize your content and reading experience through the use of cookies.  You can turn off the use of cookies at anytime by changing your specific browser settings.  We are not responsible for republished content from this blog on other blogs or websites without our permission.  This privacy policy is subject to change without notice and was last updated on Month, Day, Year. If you have any questions feel free to contact me through comment section of this blog.