Learning about delegates (again, I admit), I think I finally get it,
maybe.
I can reference any method in any class in the same namespace as long
as it has the same signature. Right?
But how does it know? Does the .NET runtime keep a list of all the
possible methods that could be called and then when the delegate is
invoked, just look for a match? For example:
A very basic example (SimpleDelegate1.cs):
using System;
namespace Akadia.BasicDelegate
{
// Declaration
public delegate void SimpleDelegate();
class TestDelegate
{
public static void MyFunc()
{
Console.WriteLine("I was called by delegate ...");
}
public static void Main()
{
// Instantiation
SimpleDelegate simpleDelegate = new SimpleDelegate(MyFunc);
// Invocation
simpleDelegate();
}
}
}
Compile an test:
# csc SimpleDelegate1.cs
# SimpleDelegate1.exe
I was called by delegate ...
(taken from:
http://www.akadia.com/services/dotne...nd_events.html)
The system just "knows" to call MyFunc because it "knows" that delegate
can work with that type of method as defined by its signature?