en estos ejemplos T reemplaza al tipo....
Ejemplo sencillo...
static void Main(string[] args)
{
int[] arrayEntero = { 1, 2, 3, 4, 5 };
double[] arrayDoble = { 1.2, 3.4, 5.6, 9.8, 10.2 };
MuestraArray(arrayEntero);
MuestraArray(arrayDoble);
Console.ReadLine();
}
private static void MuestraArray
{
foreach (T element in arrayEntrada)
Console.Write(element + " ");
Console.Write("\n");
}
}
Con restriccion...
static void Main(string[] args)
{
Console.WriteLine("maximo de {0}, {1}, {2} es {3}",3.2,4.9,3.1,Maximo(3.2,4.9,3.1));
Console.WriteLine("maximo de {0}, {1}, {2} es {3}", 3, 9, 30, Maximo(3,9,30));
Console.WriteLine("maximo de {0}, {1}, {2} es {3}", "a", "b", "c", Maximo("a","b","c"));
Console.ReadLine();
}
private static T Maximo
where T:IComparable
{
T max = x;
if (y.CompareTo(max) > 0)
max = y;
if (z.CompareTo(max) > 0)
max = z;
return max;
}
Aqui utilizamos el método genérico Máximo, de tipo T y variables tambien de tipo T, el tipo T nos quiere decir que puede ser cualquiera: int, double, string, etc.

No hay comentarios:
Publicar un comentario