Collection定义

Collection是个关于一些变量的集合,按功能可分为Lists,Dictionaries,Sets三个大类。

  1. Lists:是一个有序的集合,支持index look up。
    • Array
    • List<T>
  2. Dictionaries:同TKey store 一些TValue集合,没有特定order
  3. Sets:用来form collections into onther collection,比如HashSet<T>

Collection提供的一些公共方法

C#中的Collection 1

是Collection就至少会有Enumerat,但是不一定都有Look up(比如Sets就没有,Statcks和Queues也是有自己的固定排列顺序,不提供look up的方法),注意List的look up用index,而Dictionary的look up用Key。

For adding和removing的位置可能也不一样,list可以指定加和减的位置,而Statck是先进先出

Array

1: 有Fixed size并且是固定从上到下的顺序储存,index从0开始。

 1  static void Main(string[] args)
 2         {
 3             //Declare
 4             string[] daysOfWeek ={
 5                                     "Monday",
 6                                     "Tuesday",
 7                                     "Wednesday",
 8                                     "Thurday",
 9                                     "Friday",
10                                     "Saturday",
11                                     "Sunday"
12                                 };
13             Console.WriteLine("Type in index of day of look up>");
14             int index = int.Parse(Console.ReadLine());
15 
16             //look up using index
17             Console.WriteLine(daysOfWeek[index]);
18 
19             //Enumerates
20             foreach (string day in daysOfWeek)
21             {
22                 Console.WriteLine(day);
23             }
24 
25             //replace
26             daysOfWeek[5] = "PartyDay";
27         }
View Code

相关文章:

  • 2022-12-23
  • 2021-12-05
  • 2022-02-07
  • 2022-12-23
  • 2021-08-21
  • 2021-06-07
  • 2021-08-18
  • 2022-01-07
猜你喜欢
  • 2021-10-05
  • 2022-12-23
  • 2022-12-23
  • 2021-12-28
  • 2021-06-15
  • 2022-12-23
  • 2021-06-03
相关资源
相似解决方案