个性化阅读
专注于IT技术分析

C#如何使用集合?集合的操作用法实例解读

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

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

C#中的集合1

System.Collections.Generic类

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

类名称 描述
Dictionary<TKey, TValue> 它存储键/值对, 并提供类似于非通用Hashtable类中的功能。
List<T> 它是一个动态数组, 其功能类似于非通用ArrayList类中的功能。
Queue<T> 先进先出列表, 其功能类似于非通用Queue类中的功能。
SortedList<TKey, TValue> 它是键/值对的排序列表, 并且提供的功能类似于非通用SortedList类中的功能。
Stack<T> 它是一个先进先出的列表, 并提供与非通用Stack类相似的功能。
HashSet<T> 它是唯一元素的无序集合。这样可以防止将重复项插入到集合中。
LinkedList<T> 它允许快速插入和删除元素。它实现了经典的链表。

例子:

// C# program to illustrate the concept 
// of generic collection using List<T>
using System;
using System.Collections.Generic;
  
class Geeks {
  
     // Main Method
     public static void Main(String[] args)
     {
  
         // Creating a List of integers
         List< int > mylist = new List< int >();
  
         // 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#中的非泛型集合在系统集合命名空间。它是可用于对象引用的通用数据结构, 因此它可以处理任何类型的对象, 但不能以安全类型的方式处理。非通用集合由一组接口和类定义。下表包含了系统集合命名空间:

类名称 描述
ArrayList 它是动态数组, 表示数组的大小不固定, 可以在运行时增加和减少。
Hashtable 它表示根据键的哈希码组织的键和值对的集合。
Queue 它表示对象的先进先出集合。当你需要项目的先进先出访问时使用。
Stack 它是线性数据结构。它遵循LIFO(后进先出)模式进行输入/输出。

例子:

// 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版本4及以后。它提供了各种线程安全的收集类, 这些类可用于替换系统集合和System.Collections.Generic多个线程同时访问集合时的名称空间。此集合中提供的类是:

类名称 描述
BlockingCollection 它为实现的线程安全集合提供了阻止和限制功能。
IProducerConsumerCollection。
ConcurrentBag 它表示线程安全的对象的无序集合。
ConcurrentDictionary 它表示键/值对的线程安全集合, 多个线程可以同时访问这些键/值对。
ConcurrentQueue 它表示线程安全的先进先出(FIFO)集合。
ConcurrentStack 它表示线程安全的后进先出(LIFO)集合。
OrderablePartitioner 它代表将可排序数据源拆分为多个分区的一种特殊方式。
Partitioner 它为数组, 列表和枚举数提供了常见的分区策略。
Partitioner 它代表将数据源拆分为多个分区的一种特殊方式。

赞(0)
未经允许不得转载:srcmini » C#如何使用集合?集合的操作用法实例解读

评论 抢沙发

评论前必须登录!