An Introduction to C# Generics[9]

[入库:2005年8月18日] [更新:2007年3月25日]

本文简介:选择自 13121982 的 blog

}
int number = myclass<int>.somemethod<string>(3,"aaa");
or rely on type inference when possible:
int number = myclass<int>.somemethod(3,"aaa");
generic static methods are subjected to all constraints imposed on the generic type parameter they use
at the class level. as with instance method, you can provide constraints for generic type parameters
defined by the static method:
public class myclass
{
public static t somemethod<t>(t t) where t : icomparable<t>
{...}
}
operators in c# are nothing more than static methods and c# allows you to overload operators for your
generic types. imagine that the generic linkedlist of code block 3 provided the + operator for
concatenating linked lists. the + operator enables you to write the following elegant code:
linkedlist<int,string> list1 = new linkedlist<int,string>();
linkedlist<int,string> list2 = new linkedlist<int,string>();
...
linkedlist<int,string> list3 = list1+list2;
code block 7 shows the implementation of the generic + operator on the linkedlist class. note that
operators cannot define new generic type parameters.
code block 7. implementing a generic operator
public class linkedlist<k,t>
{
public static linkedlist<k,t> operator+(linkedlist<k,t> lhs,
linkedlist<k,t> rhs)
{
return concatenate(lhs,rhs);
}
static linkedlist<k,t> concatenate(linkedlist<k,t> list1,
linkedlist<k,t> list2)
{
linkedlist<k,t> newlist = new linkedlist<k,t>();
node<k,t> current;
current = list1.m_head;
while(current != null)
{
newlist.addhead(current.key,current.item);
current = current.nextnode;
}
current = list2.m_head;
while(current != null)
{
newlist.addhead(current.key,current.item);
current = current.nextnode;
}
return newlist;
}
//rest of linkedlist
}
generic delegates
a delegate defined in a class can take advantage of the generic type parameter of that class. for
example:
public class myclass<t>
{
public delegate void genericdelegate(t t);
public void somemethod(t t)
{...}
}
when specifying a type for the containing class, it affects the delegate as well:
myclass<int> obj = new myclass<int>();
myclass<int>.genericdelegate del;
del = new myclass<int>.genericdelegate(obj.somemethod);
del(3);
c# 2.0 allows you to make a direct assignment of a method reference into a delegate variable:
myclass<int> obj = new myclass<int>();
myclass<int>.genericdelegate del;
del = obj.somemethod;
i am calling this feature delegate inference. the compiler is capable of inferring the type of the delegate
you assign into, finding if the target object has a method by the name you specify, and verifying that the
method's signature matches. then, the compiler creates a new delegate of the inferred argument type
(including the correct type instead of the generic type parameter), and assigns the new delegate into the
inferred delegate.
like classes, structs, and methods, delegates can define generic type parameters too:
public class myclass<t>
{
public delegate void genericdelegate<x>(t t,x x);
}
delegates defined outside the scope of a class can use generic type parameters. in that case, you have
to provide the specific types for the delegate when declaring and instantiating it:
public delegate void genericdelegate<t>(t t);
public class myclass
{
public void somemethod(int number)
{...}
}
myclass obj = new myclass();
genericdelegate<int> del;
del = new genericdelegate<int>(obj.somemethod);
del(3);
alternatively, you can use delegate inference when assigning the delegate:
myclass obj = new myclass();
genericdelegate<int> del;
del = obj.somemethod;
and naturally, a delegate can define constraints to accompany its generic type parameters:
public delegate void mydelegate<t>(t t) where t : icomparable<t>;
the delegate-level constraints are enforced only on the using side, when declaring a delegate variable
and instantiating a delegate object, similar to any other constraint at the scope of types or methods.
generic delegates are especially useful when it comes to events. you can literally define a limited set of
generic delegates, distinguished only by the number of the generic type parameters they require, and
use these delegates for all of your event handling needs. code block 8 demonstrates the use of a
generic delegate and a generic event-handling method.
code block 8. generic event handling
public delegate void
genericeventhandler<sendertype,argstype>(sendertype sender,
argstype args);
public class mypublisher
{

本文关键:An Introduction to C# Generics
  相关方案
Google
 

本站最佳浏览方式为 分辨率 1024x768 IE 6.0(或更高版本的 IE浏览器)

go top