In C#, we have delegates, which are essentially variables (holders) for methods that follow a signature. So, I could write
delegate void MyDelegate(int num);
MyDelegate myMethodHolder;
Now, what's always perplexed me about this is the reason for the first line. The language designers decided upon a syntax like the one above rather than something more along the lines of
delegate myDelegate = new delegate (int) -> void
Normally, we would want something in a variable in order to either reuse it, or to be able to keep multiple versions of (potentially the same) data. But the first line in simply marks out a method signature. There would never be a need for something like:
delegate void MyDelegate(int num);
delegate void MyDelegate2(int num);
Even if I wanted to use more meaningful variable names, the value is questionable, since what we've designated is a signature:
delegate void intToVoid(int num)
So, my question then, is what is the possible advantage of storing the signature separately from the creation of myMethodHolder
?
I know that this question (as I understand it) is somewhat speculative, so I would understand if it gets closed for that reason. However, I'm hoping that someone will be able to point to a more fundamental misunderstanding on my part, in which case the question wouldn't be speculative at all :)
event
anddelegate
were designed is that they make it easy to implement the Event-based Asynchronous Pattern (EAP) -- which tended to exist in most popular Windows-based languages/frameworks even before C# existed (The pattern and similar syntax was widely used in older Microsoft and Borland technologies. e.g. VB6, MFC, Win32 API, Delphi, Borland VCL, etc.). A lot of design choices for C# and .NET Framework were about trying to encourage the entire Windows developer community along for the ride.