Function parameters are called polymorphism. Can the parameter types of a function be determined?
Can a function return only one value? Is the return value of the function uncertain?
Generic is a special type that delays the operation of a specified type to the client. Code Declare and instantiate classes or methods.
The following are two typical examples: 1. Input a string and convert it to the desired type. With the wildcard feature, the returned value can be of the specified type. 2. Compare two objects and return a large value. Using System;
Using System. Collections. Generic;
Using System. text;
Namespace Familymanage
{
Class Cgeneric
{
// Data Conversion
Static Public T convert < T > ( String S) Where T: iconvertible
{
Return (T) system. Convert. changetype (S, Typeof (T ));
}
// Take one of the two large numbers.
Static Public T max < T > (T first, t second) Where T: icomparable < T >
{
If (First. compareto (second) > 0 )
Return First;
Return Second;
}
// Use
Static Public Void Test ()
{
//
Int IMAX = Max ( 123 , 456 );
Double Dmax = Max < Double > ( 1.23 , 4.56 ); // The return type can be specified.
//
Int Iconvert = Convert < Int > ( " 123456 " );
Float Fconvert = Convert < Float > ( " 123.456 " );
//
System. Windows. Forms. MessageBox. Show (IMAX + " | " + Dmax + " | " + Iconvert + " | " + Fconvert );
}
}
}
From: http://www.it100.info/csharp-generic.html