泛型集合遍历数据(里氏转换)

    /*
            通过里氏转换进行遍历泛型集合中的对象数据.
                   
            */
            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);
            }

原文地址:https://www.cnblogs.com/haimingkaifa/p/5383917.html