//集合初始化器
    List list = new List(){1,2,3,4,5,6,7,8,};

    1. using System;
    2. using System.Collections.Generic;
    3. using System.Linq;
    4. namespace _074_List泛型集合
    5. {
    6. class Program
    7. {
    8. static void Main(string[] args)
    9. {
    10. //List 在 using System.Collections.Generic;空间下
    11. //创建泛型集合对象 List<Type> Type一旦确定,该集合只能使用该Type
    12. List<int> list = new List<int>();
    13. list.Add(0);
    14. list.Add(1);
    15. list.Add(2);
    16. list.AddRange(new int[] { 3, 4, 5, 6, 7, 8 });
    17. list.AddRange(list);
    18. for (int i = 0; i < list.Count; i++)
    19. {
    20. Console.WriteLine(list[i]);
    21. }
    22. Console.WriteLine(list);
    23. Console.WriteLine("==============================");
    24. //List泛型集合可以转换成数组
    25. int[] nums = list.ToArray();
    26. for (int i = 0; i < nums.Length; i++)
    27. {
    28. Console.WriteLine(nums[i]);
    29. }
    30. Console.WriteLine("==============================");
    31. //数组可以转换成List泛型集合
    32. List<int> list2 = nums.ToList<int>();
    33. for (int i = 0; i < list2.Count; i++)
    34. {
    35. Console.WriteLine(list2[i]);
    36. }
    37. Console.WriteLine(list2);
    38. Console.WriteLine("==============================");
    39. char[] ch = { 'a', 'b', 'c', 'd', 'e' };
    40. List<char> list3 = ch.ToList<char>();
    41. foreach (char item in list3)
    42. {
    43. Console.WriteLine(item);
    44. }
    45. Console.WriteLine("==============================");
    46. Console.ReadKey();
    47. }
    48. }
    49. }