Like most devs, have been using delegates for many years, and haven't really given them much thought. But I recently got egg on my face by assuming that delegates included a 'this' reference in the signature when referencing a class method. The below example illustrates the gap in my understanding.
public class SomeClass
{
public SomeClass(int someProperty)
{
SomeProperty = someProperty;
}
public int SomeProperty
{
get;
set;
}
// Throw in a Member field into the mix
public int ClassAdd(int x, int y)
{
return x + y + SomeProperty;
}
}
public static class SomeStaticClass
{
public static int StaticAdd(int x, int y)
{
return x + y;
}
}
Why is it that I can add both static and instance subscribers?
delegate int addDelegate(int x, int y);
class TestClass
{
delegate int addDelegate(int x, int y);
private void useDelegates()
{
addDelegate algorithm;
algorithm = SomeStaticClass.StaticAdd;
algorithm += new SomeClass(3).ClassAdd;
int answer = algorithm(5, 10);
}
}
What is actually going on ;)
Thanks!