星空网 > 软件开发 > ASP.net

C#中集合的使用

集合:可以使用集合来维护对象组。

  C#中的数组实现为 System.Array 类的实例,它们只是集合类(Collection Classes)中的一种类型。集合类一般用于处理对象列表,其功能比简单数组要多,功能大多是通过实现 System.Collections 名称空间中的接口而获得的,

  因此集合的语法已经标准化了。这个名称空间还包含其他一些有趣的东西,例如,以与 System.Array 不同的方式实现这些接口的类。集合的功能(包括基本功能,例如,用[index]语法访问集合中的项)可以通过接口来实现,

  该接口不仅没有限制我们使用基本集合类,例如 System.Array,相反,我们还可以创建自己的定制集合类。这些集合可以专用于要枚举的对象(即要从中建立集合的对象)。这么做的一个优点是定制的集

  合类可以是强类型化的。也就是说,从集合中提取项时,不需要把它们转换为正确的类型。

 

  System.Collections 命名空间中的几个接口提供了基本的组合功能: 

  IEnumerable 可以迭代集合中的项。
  ICollection(继承于 IEnumerable)可以获取集合中项的个数,并能把项复制到一个简单的数组类型中。
  IList(继承于 IEnumerable 和 ICollection)提供了集合的项列表,允许访问这些项,并提供其他一些与项列表相关的基本功能。
  IDictionary(继承于 IEnumerable 和 ICollection)类似于 IList,但提供了可通过键值(而不是索引)访问的项列表。

 

  System.Array 类实现 IList、 ICollection 和 IEnumerable,但不支持 IList 的一些更高级的功能,它表示大小固定的项列表。

  ArrayList基本使用方法如下:

 1     static void Main(string[] args) 2     { 3       //arrayList容量为10 4       ArrayList arrayList1 = new ArrayList(10); 5  6      // arrayList1[0] = 1;//此时不能通过索引来访问ArrayList否则会抛出 System.ArgumentOutOfRangeException 7       //给ArrayList添加元素 8       arrayList1.Add(1); 9       arrayList1.Add(2);10       arrayList1.Add(3);11       arrayList1.Add(4);12       arrayList1.Add("hello");13       arrayList1.Add("ArrayList");14 15       arrayList1[2] = 4;//这里可以通过索引方式访问ArrayList16 17       Console.WriteLine("arrayList1 count: {0}", arrayList1.Count);18 19       foreach (object i in arrayList1)20       {21         Console.WriteLine("arrayList1 item {0} type:{1}", i, i.GetType());22       }23 24       Console.WriteLine("remove item at index 4");25       arrayList1.RemoveAt(4);26 27       foreach (object i in arrayList1)28       {29         Console.WriteLine("arrayList1 item {0} type:{1}", i, i.GetType());30       }31 32       Console.WriteLine("remove item 2");33       arrayList1.Remove(2);34 35       foreach (object i in arrayList1)36       {37         Console.WriteLine("arrayList1 item {0} type:{1}", i, i.GetType());38       }39 40       Console.WriteLine("\n\n");41       //通过ArrayList1创建ArrayList242       ArrayList arrayList2 = new ArrayList(arrayList1);43       Console.WriteLine("arrayList2 count: {0}", arrayList1.Count);44       foreach (object i in arrayList1)45       {46         Console.WriteLine("arrayList2 item {0} type:{1}", i, i.GetType());47       }48 49       Console.WriteLine("\n\nPress any key to exit!");50       Console.ReadKey();51     }

  运行结果:

arrayList1 count: 6arrayList1 item 1 type:System.Int32arrayList1 item 2 type:System.Int32arrayList1 item 4 type:System.Int32arrayList1 item 4 type:System.Int32arrayList1 item hello type:System.StringarrayList1 item ArrayList type:System.Stringremove item at index 4arrayList1 item 1 type:System.Int32arrayList1 item 2 type:System.Int32arrayList1 item 4 type:System.Int32arrayList1 item 4 type:System.Int32arrayList1 item ArrayList type:System.Stringremove item 2arrayList1 item 1 type:System.Int32arrayList1 item 4 type:System.Int32arrayList1 item 4 type:System.Int32arrayList1 item ArrayList type:System.StringarrayList2 count: 4arrayList2 item 1 type:System.Int32arrayList2 item 4 type:System.Int32arrayList2 item 4 type:System.Int32arrayList2 item ArrayList type:System.StringPress any key to exit!

  

  自定义集合

  可以通过继承System.Collections.CollectionBase 类的方式自定义集合,CollectionBase 类有接口 IEnumerable、 ICollection 和 IList,但只提供了一些简要的实现代码,特别是 IList 的 Clear()和 RemoveAt()方法,

  以及 ICollection 的 Count 属性。如果要使用提供的功能,就需要自己执行其他代码。

  为便于完成任务, CollectionBase 提供了两个受保护的属性,它们可以访问存储的对象本身。我们可以使用 List 和 InnerList, List 可以通过 IList 接口访问项, InnerList 则是用于存储项的 ArrayList对象。

 1   public abstract class Animal 2   { 3     private string name; 4     public string Name 5     { 6       get { return name; } 7       set { name = value; } 8     } 9 10     public Animal()11     {12       name = "NULL";13     }14 15     public Animal(string newName)16     {17       name = newName;18     }19 20     public void Feed()21     {22       Console.WriteLine("{0} has been fed.", name);23     }24   }25 26   public class Dog:Animal27   {28     public void Run()29     {30       Console.WriteLine("Dog run....");31     }32 33     public Dog(string newName)34       : base(newName)35     {36 37     }38   }39 40   public class Bird : Animal41   {42     public Bird(string newName)43       : base(newName)44     {45 46     }47 48     public void Fly()49     {50       Console.WriteLine("Bird fly....");51     }52   }53 54   class Animals:CollectionBase55   {56     public void Add(Animal animal)57     {58       List.Add(animal);59     }60 61     public void Remove(Animal animal)62     {63       List.Remove(animal);64     }65 66     public Animals()67     {68 69     }70 71     //通过下面代码使得Animals可以通过索引访问72     public Animal this[int animalIndex]73     {74       get75       {76         return (Animal)List[animalIndex];77       }78       set79       {80         List[animalIndex] = value;81       }82     }83   }84 85     static void Main(string[] args)86     {87       Animals animals = new Animals();88       animals.Add(new Dog("Jack"));89       animals.Add(new Bird("Jason"));90 91       foreach (Animal animal in animals)92       {93         animal.Feed();94       }95 96       Console.WriteLine("\n\n\nPress any key to exit!");97       Console.ReadKey();98     }

  运行结果:

Jack has been fed.Jason has been fed.Press any key to exot!

  其中, Add()和 Remove()方法实现为强类型化的方法,使用 IList 接口中用于访问项的标准 Add()方法。该方法现在只用于处理 Animal 类或派生于 Animal 的类,而前面介绍的 ArrayList 实现代码可处理任何对象。

  索引符(indexer)是一种特殊类型的属性,可以把它添加到一个类中,以提供类似于数组的访问。this 关键字与方括号中的参数一起使用,但这看起来类似于其他属性。这个语法是合理的,因为在访问索引符时,将使用对象名,

  后跟放在方括号中的索引参数(例如 MyAnimals[0])。




原标题:C#中集合的使用

关键词:C#

C#
*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

新消息:https://www.goluckyvip.com/tag/6884.html
团队联系方式:https://www.goluckyvip.com/tag/6885.html
邮箱汇:https://www.goluckyvip.com/tag/6886.html
广告投放攻略:https://www.goluckyvip.com/tag/6887.html
​Amazon Live:https://www.goluckyvip.com/tag/6888.html
推广账号:https://www.goluckyvip.com/tag/6889.html
TikTok斥资210万美元游说美国参议院阻止法案通过 :https://www.goluckyvip.com/news/188220.html
北京飞机票查询(快速查询北京至各地机票价格和航班信息):https://www.vstour.cn/a/366178.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流