Using C# Delegates

C# Delegates
The concept of c# delegates brings the new feature that supports pointing or holding reference to the function in C# Programming World.
An important thing to be noted when using a delegate for a method that, it should contain the same return type or type of parameter(s), if any, that the addressed method is containing.

Why pointing by reference is more beneficial?

The concept of referencing by address is more beneficial than by referring by value because when some pointer holds the reference of a variable or a method with its address learned by it, it becomes quite efficient to invoke that method or use the variable (in case of C# pointers) for the memory management.
A real life example is finding the address of a person in order to meet him/her .
When a Delegate holds a reference to the method, it invokes that every time from that address, hence it also improves the method of following proper coding pattern by not writing the same method again and again when calling it number of times.

Delegate acts as the container of events

With that being said, a Delegate act as a Handler of event(s) in C#. Events in C# can be defined as the outcomes that occur when some action is performed.For example (pressing the button redirects the page to another page is the redirection event and pressing the button is an action.

Following examples show how to use a delegate in a C# program:


//Rextester.Program.Main is the entry point for your code. Don't change it.
//Compiler version 4.0.30319.17929 for Microsoft (R) .NET Framework 4.5

using System;

// Declaring a Delegate with integer return type and int parameter

delegate int MultiplyNumbers(int myNumber);


// Namespace Rextester
namespace Rextester
{
    public class Program
    {
     
        // initializing a static variable myNumber to value 5
        public static int myNumber=5;
        
        // The multiply method
        public static int NumberstoMultiply(int myNumber2)
        {
            
            myNumber=myNumber*myNumber2;
            return myNumber;
            
        }
        
                
        
        public static void Main(string[] args)
        {
    
            int a=2;
            
            // Callong method instance with Delegate
            
            MultiplyNumbers nm=new MultiplyNumbers(NumberstoMultiply);
            
            nm(a);
            
            Console.WriteLine("The Result of Multiplication is: {0}",myNumber);
            
            
         
        }
   
    
     
    
    
    
    
    }
}


Output:

The Result of Multiplication is: 10

Syed Adeel Ahmed
Analyst, Programmer, Educationist and Blogger at Technofranchise
Computer Systems Engineer from Sir Syed University Of Engineering & Technology.I am passionate about all types of programming.
Syed Adeel Ahmed on FacebookSyed Adeel Ahmed on GoogleSyed Adeel Ahmed on InstagramSyed Adeel Ahmed on Twitter

Published by

Syed Adeel Ahmed

Computer Systems Engineer from Sir Syed University Of Engineering & Technology.I am passionate about all types of programming.

Leave a Reply