//集合初始化器
List
using System;
using System.Collections.Generic;
using System.Linq;
namespace _074_List泛型集合
{
class Program
{
static void Main(string[] args)
{
//List 在 using System.Collections.Generic;空间下
//创建泛型集合对象 List<Type> Type一旦确定,该集合只能使用该Type
List<int> list = new List<int>();
list.Add(0);
list.Add(1);
list.Add(2);
list.AddRange(new int[] { 3, 4, 5, 6, 7, 8 });
list.AddRange(list);
for (int i = 0; i < list.Count; i++)
{
Console.WriteLine(list[i]);
}
Console.WriteLine(list);
Console.WriteLine("==============================");
//List泛型集合可以转换成数组
int[] nums = list.ToArray();
for (int i = 0; i < nums.Length; i++)
{
Console.WriteLine(nums[i]);
}
Console.WriteLine("==============================");
//数组可以转换成List泛型集合
List<int> list2 = nums.ToList<int>();
for (int i = 0; i < list2.Count; i++)
{
Console.WriteLine(list2[i]);
}
Console.WriteLine(list2);
Console.WriteLine("==============================");
char[] ch = { 'a', 'b', 'c', 'd', 'e' };
List<char> list3 = ch.ToList<char>();
foreach (char item in list3)
{
Console.WriteLine(item);
}
Console.WriteLine("==============================");
Console.ReadKey();
}
}
}