0

我知道如何枚举一个数组,但是无论类型如何,我该怎么做?

代码:

ViewArray(saClosestVehicles);

public static void ViewArray(object[] ar) {
    #region Examples (Quickly display an arrays contents)
    // object[] ar = new object[] { "blah", 4, "whatever" }; 
    // ViewArray(ar); 
    #endregion

    string strArrayContents="";
    for(int cnt=0; cnt<ar.Length; cnt++)
        strArrayContents+=ar[cnt]+" ";

    MessageBox.Show(strArrayContents, "Array Contents");
}
4

2 回答 2

0

考虑泛型声明:

public static void ViewArray<T>(T[] ar) {
    MessageBox.Show(String.Join(", ", ar.Select(x => x.ToString()).ToArray()), "Array Contents");
}
于 2013-03-14T20:18:53.283 回答
0
object[] objArray = new object[]{"blah", 4, "whatever"};

foreach(var value in objArray)
{
    // do something with value
}

this what you want?

于 2013-03-14T16:20:42.280 回答