c# – What is a delegate in C # language?


Explain in simple, human language who is and why is a delegate needed in OOP in general and in C # in particular?


A delegate is an object-oriented way of treating a method as a variable. Its more familiar counterpart is a function pointer, functor, or even just an interrupt vector (thanks @rdorn for the tip).

A delegate is a type that matches a specific function signature. Having declared a variable of a delegate type, you can write a static or non-static method to it, pass it as an argument somewhere, and call it.

A classic example of using delegates is sorting a list of objects by the value of a field. You pass in the sorting method delegate, which calculates the object sort key, ie, pulls out the value of the field.

Scroll to Top