Delegate is a reference type that holds the reference of a class method. Any method which has the same signature as delegate can be assigned to delegate. It is very similar to the function pointer but with a difference that delegates are a type-safe. We can say that it is the object-oriented implementation of function pointers.
There are three steps for defining and using delegates:
- Declaration
A delegate is declared by using the keyword delegate, otherwise it resembles a method declaration.
- Instantiation
To create a delegate instance, we need to assign a method (which has same signature as delegate) to delegate.
- Invocation
Invoking a delegate is like as invoking a regular method.
1. //1. Declaration
2. public delegate int MyDelagate(int a, int b); //delegates having same signature as method
3.
4. public class Example
5. {
6. // methods to be assigned and called by delegate
7. public int Sum(int a, int b)
8. {
9. return a + b;
10. }
11.
12. public int Difference(int a, int b)
13. {
14. return a - b;
15. }
16. }
17. class Program
18. {
19. static void Main()
20. {
21. Example obj = new Example();
22.
23. // 2. Instantiation : As a single cast delegate
24. MyDelagate sum = new MyDelagate(obj.Sum);
25. MyDelagate diff = new MyDelagate(obj.Difference);
26.
27. // 3.Invocation
28. Console.WriteLine("Sum of two integer is = " + sum(10, 20));
29. Console.WriteLine("Difference of two integer is = " + diff(20, 10));
30. }
31. }
32.
33. /* Out Put
34.
35. Sum of two integer is = 30
36. Difference of two integer is = 10
37. */
Key points about delegates
- Delegates are like C++ function pointers but are type safe.
- Delegates allow methods to be passed as parameters.
- Delegates are used in event handling for defining callback methods.
- Delegates can be chained together i.e. these allow defining a set of methods that executed as a single unit.
- Once a delegate is created, the method it is associated will never changes because delegates are immutable in nature.
- Delegates provide a way to execute methods at run-time.
- All delegates are implicitly derived from System.MulticastDelegate, class which is inheriting from System.Delegate class.
- Delegate types are incompatible with each other, even if their signatures are the same. These are considered equal if they have the reference of same method.
- Single cast delegate
Types of delegates
A single cast delegate holds the reference of only single method. In previous example, created delegate is a single cast delegate.
- Multi cast delegate
A delegate which holds the reference of more than one method is called multi-cast delegate. A multicast delegate only contains the reference of methods which return type is void. The + and += operators are used to combine delegate instances. Multicast delegates are considered equal if they reference the same methods in the same order.
1. //1. Declaration
2. public delegate void MyDelagate(int a, int b);
3. public class Example
4. {
5. // methods to be assigned and called by delegate
6. public void Sum(int a, int b)
7. {
8. Console.WriteLine("Sum of integers is = " + (a + b));
9. }
10.
11. public void Difference(int a, int b)
12. {
13. Console.WriteLine("Difference of integer is = " + (a - b));
14. }
15. }
16. class Program
17. {
18. static void Main()
19. {
20. Example obj = new Example();
21. // 2. Instantiation
22. MyDelagate multicastdel = new MyDelagate(obj.Sum);
23. multicastdel += new MyDelagate(obj.Difference);
24.
25. // 3. Invocation
26. multicastdel (50, 20);
27. }
28. }
29.
30. /* Out put
31.
32. Sum of integers is = 70
33. Difference of integer is = 30
34.
35. */
Static Constructors
A static constructor is used to initialize any static data, or to perform a particular action that needs to be performed once only. It is called automatically before the first instance is created or any static members are referenced. Static constructors have the following properties:
- A static constructor does not take access modifiers or have parameters.
- A static constructor is called automatically to initialize the class before the first instance is created or any static members are referenced.
- A static constructor cannot be called directly.
- The user has no control on when the static constructor is executed in the program.
- A typical use of static constructors is when the class is using a log file and the constructor is used to write entries to this file.
- If a static constructor throws an exception, the runtime will not invoke it a second time, and the type will remain uninitialized for the lifetime of the application domain in which your program is running.
class Cl1
{
//create static readonly object for new instance of
//this class (cl1).it is use for calling private constructor
Public static readonly ss3 Instance = new cl1 ();
Public int A;
Static cl1 (){
Console.WriteLine(“static”);
}
private cl1 ()
{
this.A = 5;
Console.WriteLine(“public1”);
}
public cl1 (string rt) {
Console.WriteLine(“public”); }
public static void MyMethod()
{
Console.WriteLine(“MyMethod invoked.”);
}
}
class Program
{
static void Main(string[] args){
//if we create new instance of class cl1 then call
//private,static and public constructor in sequence .static and private //constructor call only one time in entire assembly
cl1 pr = new cl1(“io”);
cl1.MyMethod();
}
}
Private Constructors
A private constructor is a special instance constructor. It is commonly used in classes that contain static members only. If a class has one or more private constructors and no public constructors, then other classes (except nested classes) are not allowed to create instances of this class.
The declaration of the empty constructor prevents the automatic generation of a default constructor. Note that if we don’t use an access modifier with the constructor it will still be private by default. However, the private modifier is usually used explicitly to make it clear that the class cannot be instantiated.
Private constructors are useful to prevent creation of a class when there are no instance fields or methods, such as the Math class, or when a method is called to obtain an instance of a class.
Example–
using System;
public class MyClass
{
private MyClass() {}
public static int counter;
public static int IncrementCounter()
{
return ++counter;
}
}
class MainClass
{
static void Main()
{
// If you uncomment the following statement, it will generate
// an error because the constructor is inaccessible:
// MyClass myObject = new MyClass(); // Error
MyClass.counter = 100;
MyClass.IncrementCounter();
Console.WriteLine(“New count: {0}”, MyClass.counter);
}
}