Aktuelle Änderungen Printable View Änderungen Bearbeiten
VisualStudio > KnotTying > ModPython > ComPreVal > CodeSwarm > WordPress > MapReduce > PyChecker > JavaBeans > PingBacks > PingBack > Branching > PythonXML > WikiWords > WikiWord > Junctions > DelegatesClear TrailThink of a delegate as an interface declaring exactly one method. An instantiation of a delegate is similar to an anonymous inner class that implements the interface through a one-line call to a single method (static or instance) with a compatible signature.
public class Class {
public void display(String s) { Console.WriteLine(s); }
}
public delegate void doShow(String s);
doShow item = new doShow(new Class().display);
Note that the display-method is given as an object (not an actual call) to the delegate doShow!Delegates handle problems which would be solved with function pointers in C++, and interfaces in Java. [1]
You can create a delegate to any (reference or value type) object which has a method of the right signature.
The 'event' keyword hides all the delegate's methods apart from += and -= to classes other than the class it is declared in. [2]
C# allows you to create a multicast delegate instance by using the 'event' keyword [3]
>>> class test(object):
... def method(self, o):
... self.o = o
...
>>> test.method
<unbound method test.method>
>>> test().method
<bound method test.method of <__main__.test object at 0x008D1870>>
>>> t = test()
>>> t.method
<bound method test.method of <__main__.test object at 0x008D1B70>>
>>> t.method(3)
>>> t.o
3
>>> delegate = t.method
>>> delegate(6)
>>> t.o
6