Respect the author. Keep the words www.it55.com.
C #2.0, as an upgraded version of # 1.x, introduces many new and practical features to us. The most important types are generic, anonymous methods, iterators, and partial types ). These new features provide high compatibility while greatly improving code efficiency and security.
In this section, we will learn about generics.
Necessity of existence of generics: in 1. in Version X, in order to adapt to the introduction of different types of parameters, we often need to rewrite some functions or convert them into objects to achieve the versatility of functions. But it often brings us a decline in program performance and an increase in repetitive work. This problem is well solved by the emergence of generics. In short, generics are a mechanism that can be passed or flexibly regulate the parameter type.
The generic model must be supported by the namespace system. Collections. Generic. It can be used in the design of classes, methods, structures, interfaces, and delegation. It is a combination of comprehensive, secure, and efficient types. The following example shows several usage methods of generics.
1. Generic Method
Using system;
Using system. Collections. Generic;
Public class genericmethod
{
// Static generic Method
Public static string output <t> (T)
{
Return "type:" + T. GetType (). tostring () + "; Value:" + T. tostring ();
}
}
Public partial class generic_method: system. Web. UI. Page
{
Protected void page_load (Object sender, eventargs E)
{
Response. Write (genericmethod. Output <int> (23) + "<br/> ");
Response. Write (genericmethod. Output <datetime> (datetime. Now) + "<br/> ");
}
}
2. Generic abstract classes
Using system;
Using system. Collections. Generic;
// Generic abstract class
Public abstract class genericparent
{
// Generic abstract method. The returned value is a generic type. Add a constraint to make generic x inherit from generic y.
Public Abstract X output <X, Y> (x, y) Where X: Y;
// Generic abstract method. The return value is of the string type. Add a constraint to make generic x inherit from ilistsource.
Public abstract string output2 <x> (x) Where X: system. componentmodel. ilistsource;
}
Public class genericchild: genericparent
{
// Override the generic method of the abstract class
Public override t output <t, z> (T, z)
{
Return T;
}
// Override the generic method of the abstract class
Public override string output2 <t> (T)
{
Return T. GetType (). tostring ();
}
}
Public partial class generic_abstract: system. Web. UI. Page
{
Protected void page_load (Object sender, eventargs E)
{
Genericchild GC = new genericchild ();
Response. Write (GC. Output <string, icomparable> ("AAA", "XXX "));
Response. Write ("<br/> ");
Response. Write (GC. output2 <system. Data. able> (new system. Data. datatable ()));
Response. Write ("<br/> ");
}
}
3. Generic Interfaces
Using system;
Using system. Collections. Generic;
// Generic interface
Public interface igenericinterface <t>
{
T createinstance ();
}
// Implement the generic class of the above generic interface
// Derivative constraint where T: Ti (T must inherit from Ti)
// Constructor constraint where T: New () (t can be instantiated)
Public class factory <t, Ti>: igenericinterface <Ti>
Where T: Ti, new ()
{
Public Ti createinstance ()
{
Return New T ();
}
}
Public partial class generic_interface: system. Web. UI. Page
{
Protected void page_load (Object sender, eventargs E)
{
Igenericinterface <system. componentmodel. ilistsource> factory =
New Factory <system. Data. able, system. componentmodel. ilistsource> ();
Response. Write (factory. createinstance (). GetType (). tostring ());
Response. Write ("<br/> ");
}
}
4. Generic Delegation
Using system;
Using system. Collections. Generic;
Public class genericdelegate
{
// Declare a generic delegate
Public Delegate string outputdelegate <t> (T); # P # paging title # e #
// Define a static method
Public static string delegatefun (string S)
{
Return string. Format ("Hello, {0}", S );
}
// Define a static method
Public static string delegatefun (datetime DT)
{
Return string. Format ("time, {0}", DT. tostring ());
}
}
Public partial class generic_delegate: system. Web. UI. Page
{
Protected void page_load (Object sender, eventargs E)
{
// Use generic delegation
Genericdelegate. outputdelegate <string> delegate1
= New genericdelegate. outputdelegate <string> (genericdelegate. delegatefun );
Response. Write (delegate1 ("aabbcc "));
Response. Write ("<br/> ");
// Use generic delegation (anonymous method)
Genericdelegate. outputdelegate <datetime> delegate2 = genericdelegate. delegatefun;
Response. Write (delegate2 (datetime. Now ));
}
}
In the next section, we will continue to learn the new features of C #2.0: anonymous methods, iterators, and local classes.