C# 中的集合

csharpprogrammingserver side programming更新于 2025/9/23 6:48:00

集合类是用于数据存储和检索的专用类。这些类支持堆栈、队列、列表和哈希表。大多数集合类都实现相同的接口。

以下是 C# 中的集合类 −

ArrayList

ArrayList 类表示可以单独索引的对象的有序集合。

Hashtable

Hashtable 使用键来访问集合中的元素。

SortedList

它使用键和索引来访问列表中的项目。

BitArray

它表示使用值 1 和 0 的二进制表示数组。

Stack

它表示后进先出的对象集合。

Queue

它表示先进先出的对象集合。

让我们看一个例子C# 中的 ArrayList 类 −

示例

using System;
using System. Collections;

namespace Demo {
   class Program {
      static void Main(string[] args) {
      ArrayList al = new ArrayList();

      al.Add(99);
      al.Add(76);
      al.Add(87);
      al.Add(46);
      al.Add(55);

      Console.WriteLine("Capacity: {0} ", al.Capacity);
      Console.WriteLine("Count: {0}", al.Count);

      Console.Write("Elements: ");
         foreach (int i in al) {
            Console.Write(i + " ");
         }

         Console.WriteLine();
         Console.ReadKey();
      }
   }
}

输出

Capacity: 8
Count: 5
Elements: 99 76 87 46 55

相关文章