泛型练习

  1. 实现装箱和拆箱操作  数值类型赋值给object、object赋值给数值

  2. ArrayList的使用

    (1)创建一个ArrayList对象

    (2)Add向ArrayList对象添加各种类型的元素

    (3)删除元素  Remove、RemoveAt

    (4)制定位置插入元素Insert

    (5)循环遍历,打印ArrayList对象中的元素,并且答应集合中元素的个数Count

  3. 创建强类型List<T>,string和int类型,重复上面的同样的操作。

  4. namespace ConsoleApp1
    
    {
    
        class Program
    
        {
    
    
            static void Main(string[] args)
    
            {
    
                int i = 4;
    
                object x = (object)i;
    
                Console.WriteLine(x);
    
                int y = (int)x;
    
    
    
                ArrayList arr = new ArrayList();
    
                Person p = new Person();
    
                //arr.Add(p);
    
                arr.Add(4);
    
                arr.Add("hello");
    
                arr.Add(5.6);
    
    
    
                foreach(object item in arr)
    
                {
    
                    Console.WriteLine(item);
    
                }
    
    
    
                List<int> intArr = new List<int>();
    
                arr.Add(5);
    
                Console.WriteLine(intArr.Count);
    
                arr.Remove(5);
    
                Console.WriteLine(intArr.Count);
    
    
                List<string> stringArr = new List<string>();
    
                stringArr.Add("hello");
    
                stringArr.Add("world");        
    
    
                List<Person> personArr = new List<Person>();
    
                personArr.Add(p);
    
    
                Console.ReadKey();
    
            }
    
        }
    
        public class Person
    
        {
    
        }
    
    }

    运行结果:

原文地址:https://www.cnblogs.com/programme-maker/p/12020959.html