What are delegates in .Net and why do we need them?
There are few good books which cover everything about delegate and how they work but one thing which was missing in all of them was why do we need delegates and how the concept of delegates has originated. In this article I will try to cover this.
For the internal working of delegates you can refer to these books:
CLR via C# by Jeffery Richter
C# in Depth by Jon Skeet
The programming world consists of states and operations.
State is the state of an object. An object consists of member fields which holds the state of the object.
For example an object of class Employee will have members as Name, DateOfBirth,
Project, Department etc. These members together hold the state of an employee object.
An action is an operation which when done on an object moves it from one state to
another. For example ChangeProject operation on an employee assigns a new project
to the employee thus moving the state of the employee object from one to another.
In programming languages these operations are represented by methods or functions.
This state of the object can be passed as a parameter, can be taken in a variable
so that the variable refers the state, can be returned from a method. Can we do the
same with operations? Can we pass operations as parameter to a method, or hold it
in a variable or return from a method as return value?
Delegates provide answer to these questions.
The way we can hold the state of an object represented by a variable of that class
we can have delegate variables which can refer to operations of a class.
So are we saying these delegate variables are the pointers to the methods the way we have function pointers in C. Well, to a good extent yes but delegates are much more than just method pointers. A delegate primarily consists of a pointer to the method and a pointer to the instance of class on which it will act upon. A delegate variable when called not only knows which method to execute but also knows which instance object to act on if instance members are referred in the method.
Since the delegate is supposed to hold a method and the object it acts upon the best choice to implement them internally is class. So all delegates are nothing but class primarily having two member fields for holding the method pointer and object pointer. For static methods the object pointer is set as null.
C# compiler also ensures that type safety is maintained on the usage of delegate variable. A delegate with specific signature of the method that it represents can not be coerced to represent a method with different signature.
For the internal working of delegates you can refer to these books:
CLR via C# by Jeffery Richter
C# in Depth by Jon Skeet
The programming world consists of states and operations.
State is the state of an object. An object consists of member fields which holds the state of the object.
For example an object of class Employee will have members as Name, DateOfBirth,
Project, Department etc. These members together hold the state of an employee object.
An action is an operation which when done on an object moves it from one state to
another. For example ChangeProject operation on an employee assigns a new project
to the employee thus moving the state of the employee object from one to another.
In programming languages these operations are represented by methods or functions.
This state of the object can be passed as a parameter, can be taken in a variable
so that the variable refers the state, can be returned from a method. Can we do the
same with operations? Can we pass operations as parameter to a method, or hold it
in a variable or return from a method as return value?
Delegates provide answer to these questions.
The way we can hold the state of an object represented by a variable of that class
we can have delegate variables which can refer to operations of a class.
So are we saying these delegate variables are the pointers to the methods the way we have function pointers in C. Well, to a good extent yes but delegates are much more than just method pointers. A delegate primarily consists of a pointer to the method and a pointer to the instance of class on which it will act upon. A delegate variable when called not only knows which method to execute but also knows which instance object to act on if instance members are referred in the method.
Since the delegate is supposed to hold a method and the object it acts upon the best choice to implement them internally is class. So all delegates are nothing but class primarily having two member fields for holding the method pointer and object pointer. For static methods the object pointer is set as null.
C# compiler also ensures that type safety is maintained on the usage of delegate variable. A delegate with specific signature of the method that it represents can not be coerced to represent a method with different signature.
Comments
Post a Comment