Delegates aren't directly about improving performance - they are about abstracting invocation. In C++ terms, it is indeed like a method pointer.
Most uses of delegates are not related to performance. Let's be clear about that from the outset.
However, one main time this can be used to help with performance is that this allows for scenarios like meta-programming. The code (usually library code) can construct complex chains of behaviours based on configuration information at runtime, and then compile that information into a method via any of Expression
, TypeBuilder
or DynamicMethod
(or basically any API that lets you construct IL). But to invoke such a dynamically generated method, you need a delegate - because your static IL that was compiled from C# can't refer to a method that didn't exist at the time.
Note that an alternative way to do this would be to use TypeBuilder
to create (at runtime) a new type that inherits from a subclass or implements a known interface, then create an instance of the dynamically generated type, which can be cast to the expected API in the usual manner and invoked normally.