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 work of a specified type until the client Code declares and instantiates a class or method.
The following are two classic examples:
1. Enter 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); // you can specify the return type.
//
Int iConvert = Convert <int> ("123456 ");
Float fConvert = Convert <float> ("123.456 ");
//
System. Windows. Forms. MessageBox. Show (iMax + "|" + dMax + "|" + iConvert + "|" + fConvert );
}
}
}