/*
通過裡氏轉換進行遍歷泛型集合中的對象數據.
*/
Person p = new Person();
ArrayList list = new ArrayList();
list.Add(1);
list.Add("Hello,World");
list.Add('C');
list.Add(p);
list.Add(new int[] { 1, 2, 3, 4, 5, 6 }); //數組類的最好用list.AddRange(new int[]{1,2,3,4,5,6});
list.Add(list); // 集合類的最好用list.AddRange(list);
for (int i = 0; i < list.Count; i++)
{
if(list[i] is Person)
{
((Person)list[i]).SayHello("花花");
}
else if(list[i] is int[])
{
for (int j = 0; j <((int[])list[i]).Length; j++)
{
Console.WriteLine(((int[])list[i])[j]);
}
}
else
{
Console.WriteLine(list[i]);
}
}
Console.ReadKey();
}
class Person
{
public string _name;
public string Name
{
get
{
return _name;
}
set
{
_name = value;
}
}
public void SayHello(string name)
{
this.Name = name;
Console.WriteLine("你好世界{0}",this.Name);
}