By 22/07/2015 1 Comments

Delegates In C#

  • Delegates in C# are the members of a class as well as namespace because they can be declared inside both class and namespace scopes.
  • A delegate is an object which refers to the methods. In other words the methods are assigned into a delegate. A delegate can hold methods.
  • The methods which are assigned to a delegate is passed from its constructor as an arguments.
  • Once the methods are assigned to a specific delegate then that methods can be called through that delegate reference.
  • Notice that a same delegate can calls to different methods by simply changing their reference to delegate.
  • The methods which are called through a delegate reference are called at run-time not compile time.
  • A delegate can refers only those methods who’s parameter-list and return type must match with that delegate.
  • Both static and instance methods can be assigned into a delegate, delegate only looks the matching of method’s return type and parameters.
  • Access modifiers can be used with delegates.
  • Delegates support events.

General Syntax:

delegate return-type delegateName (parameterList);

STATEMENT DESCRIPTION
delegate A delegate keyword.
return-type Any data type such as int, string, double etc.
delegateName It should be any meaningful name.
(parameterList) Optional local variables.

Example – 1:

using System;
namespace csharpAdvance
{
    // String type delegate declaration with empty parameters.
    delegate string DisplayData();

    class Technologies
    {
        // 2 string type method declaration.
        public string DisplayTechnology1()
        {
            return "Technology is ASP.NET.";
        }
        public string DisplayTechnology2()
        {
            return "Technology is PHP.";
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            // An object creation.
            Technologies techs = new Technologies();

            /* Delegate object is being created, 
               pass class method DisplayTechnology1() in it as a parameter. */
            DisplayData displayData = new DisplayData(techs.DisplayTechnology1);

            // Delegate reference is being assigned to string variable.
            string displayTech1 = displayData();

            // Print string variable.
            Console.WriteLine("Mechod 1 contains: {0}", displayTech1);

            displayData = new DisplayData(techs.DisplayTechnology2);
            string displayTech2 = displayData();
            Console.WriteLine("Mechod 2 contains: {0}", displayTech2);

            Console.ReadKey();
        }
    }
    /*  The Output will be:
     
        Mechod 1 contains: Technology is ASP.NET.
        Mechod 2 contains: Technology is PHP.
     */
}


Example – 2:

using System;
namespace csharpAdvance
{
    // 2 different type parametrized delegates declaration.
    delegate int GetDivision(int number, int devidedBy);
    delegate void GetSquare(int number);

    class Calculation
    {
        // An int type static method.
        public static int Division(int number, int devidedBy)
        {
            return number / devidedBy;
        }

        // A void type static method.
        public static void Square(int number)
        {
            Console.WriteLine("Square of " + number + " is: " + (number * number));
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            // An object creation of GetDivision delegate which passes Division() method as its parameter.
            GetDivision getDivision = new GetDivision(Calculation.Division);

            // Division() method is called through above delegate reference.
            Console.WriteLine("Division result: {0}", getDivision(10, 5));

            /* Following statement which assigns Square() method into a delegate getSquare 
               without a keyword new or constructor. 
              It is also a valid way and called method group conversion.*/
            GetSquare getSquare = Calculation.Square;

            // Square() method is called through getSquare delegate reference.*/
            getSquare(5);
            Console.ReadKey();
        }
    }
    /*  The Output will be:
     
        Division result: 2
        Square of 5 is: 25
     */
}





Next Tutorial →


← Previous Tutorial


Posted in: C# Advance, C#.NET

Leave your valuable Comment

Have a natural attraction for women cosmetics and replica watches uk clothes, no
more than two for men the most attractive one, is to make their own driving experience, happy and can serve as the facade of the car, another is to highlight the taste edify sentiment rolex replica watch. The replica rolex is undoubtedly the most fashionable accessories, wear a suit to attend the activities, but also get a decent match on the replica watches .