1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5
6 namespace ListDemo
7 {
8 class Program
9 {
10 static void Main(string[] args)
11 {
12 //泛型集合的基本使用
13 List<int> scoreList = new List<int>();
14 scoreList.Add(98);
15 scoreList.Add(28);
16 scoreList.Add(38);
17 scoreList.Add(78);
18 scoreList.Add(88);
19 //List<string> nameList = new List<string>();
20 scoreList.Insert(2, 70);
21 //获取元素总数
22 Console.WriteLine("获取元素总数:"+scoreList.Count );
23 Console.WriteLine("_________________");
24 //Console.ReadLine();
25 //遍历集合
26 Console.WriteLine("遍历输出在第一种方法:");
27 foreach (int score in scoreList)
28 {
29 Console.WriteLine(score);
30 }
31 Console.WriteLine("_________________");
32 Console.WriteLine("遍历输出在第二种方法:");
33 for (int i = 0; i < scoreList.Count; i++)
34 {
35 Console.WriteLine(scoreList[i]);
36 }
37 //删除一个元素
38 Console.WriteLine("_________________");
39 Console.WriteLine(" 删除一个元素");
40 scoreList.Remove(70);
41 scoreList.Remove(2);
42 for (int i = 0; i < scoreList.Count; i++)
43 {
44 Console.WriteLine(scoreList[i]);
45 }
46 Console.WriteLine("_________________");
47 Console.WriteLine("清除所有元素");
48 scoreList.Clear();
49 Console.WriteLine("获取元素总数:" + scoreList.Count);
50 Console.ReadLine();
51 }
52 }
53 }
View Code
运行结果:
标签:Console,C#,List,Add,scoreList,WriteLine,泛型,using From: https://blog.51cto.com/u_12277263/5811174