csharp-delegates

by Ismail Sirma


Posted on 9.8.2018 20:04:24


C# Delegates

Delegate is an object that knows how to call a method or group of methods. It is basically a reference (pointer) to a function. Delegates are needed to create extensible and flexible functions. Therefore, they can be used in frameworks. When the code is changed, only the changed class needs to be recompiled so that it reduces compilation time.

An example of a delegate (named as PhotoFilterHandler) :

Public delegate void PhotoFilterHandler(Photo photo);

It points to functions that have this signature.

 

You can pass delegate into a method as an input and then you can call delegate. This way you don’t have to know what type of object you will apply.

Example usage:

                Public delegate void PhotoFilterHandler(Photo photo);

                Public void Process(string path, PhotoFilterHandler filterHandler)

                {

                               var photo = Photo.Load(path);

                               filterHandler(Photo);

                               photo.Save();

                }

In this sample, you can use any filterhandler you may choose. How this sample framework code is used shown below:

Public static void Main(string[] args)

{

                Var processor = new PhotoProcessor();

                Var filter = new PhotoFilters();

                PhotoProcessor.PhotoFilterHandler filterHandler = filters.ApplyBrightness;

                PhotoProcessor.PhotoFilterHandler filterHandler += filters.ApplyContrast;

                Processor.Process(“photo.png”, filterHandler);

}

You can add new method within the class into delegate simply by += operator :

                FilterHandler += RemoveRedEyeFilter;

 

Sample delegate functions:

 

public class PhotoFilters

    {

        public PhotoFilters()

        {

        }

 

        public void ApplyBrightness(Photo photo)

        {

            System.Console.WriteLine("Apply brightness");

        }

 

        public void ApplyContrast(Photo photo)

        {

            Console.WriteLine("Apply contrast");

        }

    }

 

Every delegate in .NET is essentially a class. That class is derived from System.Delegate.

Multiple functions can be applied to delegates under invocation list. MultiCastDelegate can have multiple function pointers.

 

Existing Delegates in .NET

System.Action<> : Doesn’t return a value. It can point to 16 different methods with different inputs.

System.Func<>  : Returns a value with out option.

 

Using generic action delegate:

Public class PhotoProcessor

{

Public void Process(string path, Action<Photo> filterHandler )

{

Var photo = Photo.Load(path);

FilterHandler(photo);

Photo.Save();

}

}

 

class Program

{

Static void Main(string[] args)

{

var processor = new PhotoProcessor();

var filters = new PhotoFilters();

// Use action as a delegate

Action<Photo> filterHandler = filters.ApplyBrightnes;

processor.Process(“photo.jpeg”, filterHander);

}

}

 

Interfaces or Delegates ?

Use delegates when:

·         An eventing design pattern is used

·         The caller doesn’t need to access other properties or methods on the object implementing the method.

Blog Search

Message

Welcome to Blog of Ismail Sirma.

Back to List