We have all learned how to implement reference types using language constructs like:
These reference types allow programmers to create instances of objects and use them in special ways for development procedure purposes.
Classes allow us to create objects that contain members with attributes or functionality. While on the other hand the interface allows us to declare a set of attributes and behavior that all objects are implementing for exposing them publicly or privately.
Now I am introducing a new reference type, "delegate".
A delegate is a C# language element that allows programmers to reference a method for their development work. Generally C or C++ programmers are much more familiar with the concept of delegates in comparison to others.
C# Delegates Features
- Maximum Flexibility
- Several built-in functionalities at runtime
- Manipulation of variables
C# Delegates Description
Delegates provide flexibility and scalability, so that it can be used consistent with the requirements or goals of your development procedure. There are fewer boundary limitations with delegates. Interfaces also can be used instead of delegates but due to some extra functionality programmers do prefer delegates instead of other reference methods.
C# Delegates Declaration
Declaration of delegates is somewhat similar to methods. Except that a delegate has the extra feature of a delegate modifier. A delegate modifier is terminated with a semicolon (;) and requires no implementation.
For example:
public delegate int Comparer(object obj1, object obj2)
C# Delegates Delegate Handler
A delegate handler method works as follows:
- Public static int CompareFirstNames(object name1, object name2);
- {
- ……
- }
Reference Example
- using System;
-
- public delegate int Comparer(object obj1, object obj2);
- public class Name
- {
- public string FirstName = null;
- public string LastName = null;
-
- public Name(string first, string last)
- {
- FirstName = first;
- LastName = last;
- }
- public static int CompareFirstNames(object name1, object name2)
- {
- string n1 = ((Name)name1).FirstName;
- string n2 = ((Name)name2).FirstName;
- if (String.Compare(n1, n2) > 0)
- {
- return 1;
- }
- else if (String.Compare(n1, n2) < 0)
- {
- return -1;
- }
- else
- {
- return 0;
- }
- }
- public override string ToString()
- {
- return FirstName + " " + LastName;
- }
- }
- class SimpleDelegate
- {
- Name[] names = new Name[6];
- public SimpleDelegate()
- {
- names[0] = new Name("abhishek", "jaiswal");
- names[1] = new Name("ashish", "verma");
- names[2] = new Name("gopi", "chand");
- names[3] = new Name("hari", "prakash");
- names[4] = new Name("prashant", "kumar");
- names[5] = new Name("pratiyush", "anand");
- }
- static void Main(string[] args)
- {
- SimpleDelegate sd = new SimpleDelegate();
- Comparer cmp = new Comparer(Name.CompareFirstNames);
- Console.WriteLine("\nBefore Sorting: \n");
- sd.PrintNames();
- sd.Sort(cmp);
- Console.WriteLine("\nAfter Sorting: \n");
- sd.PrintNames();
- }
- public void Sort(Comparer compare)
- {
- object temp;
- for (int i = 0; i < names.Length; i++)
- {
- for (int j = i; j < names.Length; j++)
- {
- if (compare(names[i], names[j]) > 0)
- {
- temp = names[i];
- names[i] = names[j];
- names[j] = (Name)temp;
- }
- }
- }
- }
- public void PrintNames()
- {
- Console.WriteLine("Names: \n");
- foreach (Name name in names)
- {
- Console.WriteLine(name.ToString());
- }
- }
- }