C # Generics are not template classes for C + + and do not support specificity and biasing, but some techniques can be used to achieve the same purpose in some way.
The original is a PO on the StackOverflow of a reply: A:generic indexer overload specialization
One, the specificity of the generic method
You can use a non-generic helper class and an inline generic class to achieve the specificity of a generic method.
1 Internal Static classIndexerimpl//non-generic static helper class2 {3 Private StaticT indexerdefaultimpl<t> (inti)/=default(T);//Default Implementation4 5 Private StaticT indexerimpl2<t> (inti)/=default(T);//another implementation for Short/int/long6 7 Private Static stringIndexerforstring (inti) = = (I * i). ToString ();//Specialization for t=string8 Private StaticDateTime Indexerfordatetime (inti)/=NewDateTime (i * i * i);//Specialization for T=datetime9 Ten StaticIndexerimpl ()//Install the specializations One { Aspecializer<string. Fun =indexerforstring; -Specializer<datetime>. Fun =Indexerfordatetime; - thespecializer< Short. Fun = indexerimpl2< Short>; -specializer<int. Fun = indexerimpl2<int>; -specializer<Long. Fun = indexerimpl2<Long>; - } + - Internal Static classSpecializer<t>//Specialization Dispatcher + { A Internal Staticfunc<int, t>Fun ; at Internal StaticT Call (inti) -=NULL!= Fun -?Fun (i) -: indexerdefaultimpl<t>(i); - } - } in - Public classYourclass<t> to { + PublicT This[intI] = indexerimpl.specializer<t>. Call (i); -}
If you need an incoming instance to evaluate the returned results, you can add a parameter:
1 Internal Static classIndexerimpl//non-generic static helper class2 {3 Private StaticT indexerdefaultimpl<t> (intI, yourclass<t> yourclass) =default(T);//Default Implementation4 5 Private StaticT indexerimpl2<t> (intI, yourclass<t> yourclass) =default(T);//another implementation for Short/int/long6 7 Private Static stringIndexerforstring<t> (intI, yourclass<t> yourclass) = (i * i). ToString ();//Specialization for t=string8 Private StaticDateTime indexerfordatetime<t> (intI, yourclass<t> yourclass) =NewDateTime (i * i * i);//Specialization for T=datetime9 Ten StaticIndexerimpl ()//Install the specializations One { Aspecializer<string. Fun =indexerforstring; -Specializer<datetime>. Fun =Indexerfordatetime; - thespecializer< Short. Fun =IndexerImpl2; -specializer<int. Fun =IndexerImpl2; -specializer<Long. Fun =IndexerImpl2; - } + - Internal Static classSpecializer<t>//Specialization Dispatcher + { A Internal Staticfunc<int, Yourclass<t>, t>Fun ; at Internal StaticT Call (intI, yourclass<t>YourClass) -=NULL!= Fun -?Fun (i, YourClass) - : Indexerdefaultimpl (i, yourclass); - } - } in - Public classYourclass<t> to { + PublicT This[intI] = Indexerimpl.specializer<t>. Call (I, This); -}
Ii. partial specificity of generic methods
The same is true of partial localization, except that the helper class becomes a generic class consisting of types that do not need to be special:
1 Internal Static classGetvalueimpl<r, s>2 {3 Private StaticT defimpl<t> (R R, s s) = =default(T);4 Private Static intIntret (R R, s s) = =int. MaxValue;5 6 Internal Static classSpecializer<t>7 {8 Internal StaticFunc<r, S, t>Fun ;9 Internal StaticT Call (R R, s s) = =NULL! = fun? Fun (R, s): defimpl<t>(R, s);Ten } One A StaticGetvalueimpl () - { -specializer<int. Fun =intret; the } - } - - Public classTestClass + { - PublicT Getvalue<r, S, t> (R R, s s) = Getvalueimpl<r, S>. Specializer<t>. Call (R, s); +}
In the code snippet above, the GetValue method is the type of T parameter, when T=int, the actual method is called Getvalueimpl.intret method, the other case is the Getvalueimpl.defimpl method.
Third, the special type of generic class
There is no good way to achieve the specificity of a generic class, it can only be implemented indirectly in a way that inherits the special type generic class, and the members that will be working with the special process use virtual methods or hide base class methods with new.
Generics generic classes can also be implemented in almost the same way.
You can refer to this answer on StackOverflow: a:c# specialize generic class
C # Generic-specific