展开全部
static void GetSum(T[] a)
{
if (a == null)
{
Console.WriteLine("无意义");
return;
}
if (a is int[])
{
int sum = 0;
for (int i = 0; i < a.Length; i++)
{
sum += Convert.ToInt32(a[i]);
}
Console.WriteLine(sum);
}
else if (a is double[])
{
double sum = 0;
for (int i = 0; i < a.Length; i++)
{
sum += Convert.ToDouble(a[i]);
}
Console.WriteLine(sum);
}
else if (a is float[])
{
float sum = 0;
for (int i = 0; i < a.Length; i++)
{
sum += Convert.ToSingle(a[i]);
}
Console.WriteLine(sum);
}
}//使用32313133353236313431303231363533e59b9ee7ad9431333335303533时:
int[] a ={ 1, 2, 3, 4 };
GetSum(a);
//本来是想返回T类型的 结果一直无法实现