using System;
namespace Test
{
class Simple
{
static public void ReverseAndPrint<T>(T[] arr) //泛型方法
{
Array.Reverse(arr);
foreach (T item in arr)
Console.Write("{0}, ", item.ToString());
Console.WriteLine();
}
}
class Program
{
static void Main(string[] args)
{
//创建各种类型的数组
var intArray = new int[] { 3, 5, 7, 9, 11 };
var stringArray = new string[] { "first", "second", "third" };
var doubleArray = new double[] { 3.567, 7.891, 2.345 };
Simple.ReverseAndPrint<int>(intArray); //调用方法
Simple.ReverseAndPrint(intArray); //推断类型并调用
Simple.ReverseAndPrint<string>(stringArray);
Simple.ReverseAndPrint(stringArray);
Simple.ReverseAndPrint<double>(doubleArray);
Simple.ReverseAndPrint(doubleArray);
Console.ReadLine();
}
}
}
运行结果:
11, 9, 7, 5, 3,
3, 5, 7, 9, 11,
third, second, first,
first, second, third,
2.345, 7.891, 3.567,
3.567, 7.891, 2.345,