📜  C#中的集合

📅  最后修改于: 2021-05-29 22:15:06             🧑  作者: Mango

集合标准化了程序处理对象的方式。换句话说,它包含一组类以通用方式包含元素。借助集合,用户可以对对象执行多种操作,例如存储,更新,删除,检索,搜索,排序等。

C#将collection分为几个类,一些常见的类如下所示:

System.Collections.Generic类

C#中的通用集合在System.Collection.Generic命名空间中定义。它提供了标准数据结构的通用实现,例如链表,堆栈,队列和字典。这些集合是类型安全的,因为它们是通用的,意味着只有那些与集合类型兼容的项目才可以存储在通用集合中,这消除了意外的类型不匹配。通用集合由一组接口和类定义。下表包含System.Collections.Generic命名空间的常用类:

Class name Description
Dictionary It stores key/value pairs and provides functionality similar to that found in the non-generic Hashtable class.
List It is a dynamic array that provides functionality similar to that found in the non-generic ArrayList class.
Queue A first-in, first-out list and provides functionality similar to that found in the non-generic Queue class.
SortedList It is a sorted list of key/value pairs and provides functionality similar to that found in the non-generic SortedList class.
Stack It is a first-in, last-out list and provides functionality similar to that found in the non-generic Stack class.
HashSet It is an unordered collection of the unique elements. It prevent duplicates from being inserted in the collection.
LinkedList It allows fast inserting and removing of elements. It implements a classic linked list.

例子:

// C# program to illustrate the concept 
// of generic collection using List
using System;
using System.Collections.Generic;
  
class Geeks {
  
    // Main Method
    public static void Main(String[] args)
    {
  
        // Creating a List of integers
        List mylist = new List();
  
        // adding items in mylist
        for (int j = 5; j < 10; j++) {
            mylist.Add(j * 3);
        }
  
        // Displaying items of mylist
        // by using foreach loop
        foreach(int items in mylist)
        {
            Console.WriteLine(items);
        }
    }
}
输出:
15
18
21
24
27

System.Collections类

C#中的非泛型集合是在System.Collections命名空间中定义的。它是可用于对象引用的通用数据结构,因此它可以处理任何类型的对象,但不能以安全类型的方式处理。非通用集合由一组接口和类定义。下表包含System.Collections命名空间的常用类:

Class name Description
ArrayList It is a dynamic array means the size of the array is not fixed, it can increase and decrease at runtime.
Hashtable It represents a collection of key-and-value pairs that are organized based on the hash code of the key.
Queue It represents a first-in, first out collection of objects. It is used when you need a first-in, first-out access of items.
Stack It is a linear data structure. It follows LIFO(Last In, First Out) pattern for Input/output.

例子:

// C# to illustrate the concept
// of non-generic collection using Queue
using System;
using System.Collections;
  
class GFG {
  
    // Driver code
    public static void Main()
    {
  
        // Creating a Queue
        Queue myQueue = new Queue();
  
        // Inserting the elements into the Queue
        myQueue.Enqueue("C#");
        myQueue.Enqueue("PHP");
        myQueue.Enqueue("Perl");
        myQueue.Enqueue("Java");
        myQueue.Enqueue("C");
  
        // Displaying the count of elements
        // contained in the Queue
        Console.Write("Total number of elements present in the Queue are: ");
  
        Console.WriteLine(myQueue.Count);
  
        // Displaying the beginning element of Queue
        Console.WriteLine("Beginning Item is: " + myQueue.Peek());
    }
}
输出:
Total number of elements present in the Queue are: 5
Beginning Item is: C#

注意: C#还提供了一些专用集合,这些专用集合经过优化以处理特定类型的数据类型,并且专用集合位于System.Collections.Specialized命名空间中。

System.Collections.Concurrent

它来自.NET Framework Version 4及更高版本。当多个线程同时访问集合时,它提供了各种线程安全的集合类,这些类可替代System.CollectionsSystem.Collections.Generic此集合中提供的类是:

Class name Description
BlockingCollection It provides blocking and bounding capabilities for thread-safe collections that implement
IProducerConsumerCollection.
ConcurrentBag It represents a thread-safe, an unordered collection of objects.
ConcurrentDictionary It represents a thread-safe collection of key/value pairs that can be accessed by multiple threads concurrently.
ConcurrentQueue It represents a thread-safe first in-first out (FIFO) collection.
ConcurrentStack It represents a thread-safe last in-first out (LIFO) collection.
OrderablePartitioner It represents a particular manner of splitting an orderable data source into multiple partitions.
Partitioner It provides common partitioning strategies for arrays, lists, and enumerables.
Partitioner It represents a particular manner of splitting a data source into multiple partitions.