I`m having some trouble in understanding how delegates in C# work. I have many code examples, but i still could not grasp it properly.
Can someone explain it to me i
Its an inversion principle. Normally, you write code that calls a method and the method you call is known at the time that you write the code. Delegates allow you to anonymously invoke methods. That is you do not know the actual method that is called when the program is running.
It is useful in separating the concerns of different parts of an application. So you can have some code that performs tasks on a data store. You may have other code that processes the data. The processes on the data need not know the structure of the data store and the data store should not be dependant on the uses of the data.
The processing code can be written assuming certain things about the data that are independant of the structure of the data store. That way, we can change the structure of the data store with less worry about affecting the processes on the data.