Книги автора: C# 2008 Programmer
Книга: C# 2008 Programmer
Generic Delegates
Generic Delegates
You can also use generics on delegates. The following class definition contains a generic delegate, MethodDelegate
:
public class SomeClass<T> {
public delegate void MethodDelegate(T t);
public void DoSomething(T t) {
}
}
When you specify the type for the class, you also need to specify it for the delegate:
SomeClass<int> sc = new SomeClass<int>();
SomeClass<int>.MethodDelegate del;
del = new SomeClass<int>.MethodDelegate(sc.DoSomething);
You can make direct assignment to the delegate using a feature known as delegate inferencing, as the following code shows:
del = sc.DoSomething;