C#中的集合介绍
在C#中,我们可以通过各种方式创建和管理相关对象。例如,通过使用数组或集合。
什么是C#中的集合?
集合是一组类型相似的对象,我们可以对其进行插入、删除、更新、排序等操作。
与数组相比,集合是处理一组相关项目或对象的一种更有效的方法。
本文的目的是指导你在C#编程语言中如何使用集合(类型)。
集合通常用于处理和管理一组类似的数据类型。数组也被用来管理类似类型的数据,但在处理不同类型的分组项目时,集合提供了额外的灵活性。
集合的类型
C#集合被分为3个主要命名空间。
System.Collections.Generic classes (Generic)System.Collections.Concurrent classes (Concurrent)System.Collections classes (Non-Generic)
为了将元素添加到一个集合中,我们声明一个类的实例。
例如,当我们使用相同数据类型的元素时,我们声明System.Collections.Generic 命名空间,它将导入所有需要的类。
System.Collections.Generic类(通用类)
如果元素的数据类型相同,我们使用System.Collections.Generic 名称空间中的一个类。通用集合只接受一种数据类型,不接受其他数据类型。
泛型集合提供的类包括。
ListStackQueueLinkedListHashSetSortedSetDictionarySortedDictionarySortedList
System.Collections.Concurrent类(并发)。
在.NET Framework 4中,通过System.Collections.Concurrent 命名空间支持并发集合。它们有助于从多个线程访问集合对象。
如果许多线程需要同时访问一个集合,那么应该使用并发集合来代替非通用和通用集合。
并发集合所提供的类包括。
BlockingCollectionConcurrentBagConcurrentStackConcurrentQueueConcurrentDictionaryPartitionerOrderablePartitioner
System.Collections类(非通用型)
与泛型集合不同,非泛型集合接受不同数据类型的元素,并利用System.Collections 名称空间。
非泛型集合提供的类包括:。
ArrayListStackQueueHashtable
让我们来看看它们中的每一个。
ArrayList
与有固定大小的Array 不同,ArrayList 是动态的。因此,它没有一个固定的大小。当更多的元素被添加到集合中时,ArrayList 就会扩展,并且可以容纳不同数据类型的元素。
让我们来看看一个ArrayList 的例子。
using System;
using System.Collections;
class MainClass
{
public static void Main(string[] args)
{
var data = new ArrayList();
data.Add(" ArrayList"); // adding elements in a collection
data.Add(221);
data.Add(23);
data.Add(new OurArrayList());
data.Remove(23);
foreach (object el in data)
{
Console.WriteLine(el); // displaying the elements
}
}
}
class OurArrayList {}
运行上述代码段将显示如下。
ArrayList
221
OurArrayList
在上面的代码中,我们创建了一个ArrayList ,并添加了不同数据类型的元素,即:string,int, 和一个类对象。
下面是代码的分解。
using System.Collections;
我们已经用System.Collections 来利用ArrayList 集合。
var data = new ArrayList();
我们已经做了一个ArrayList 集合。
data.Add(" ArrayList"); // for adding elements in a collection
data.Add(221);
data.Add(23);
data.Add(new OurArrayList()); // It adds a class
我们使用了Add() 方法来添加元素到集合中。
data.Remove(23);
在这里,我们使用了Remove() 方法来从集合中删除一个元素。
堆栈
Stack是一个很好的例子,它反映了 "后进先出 "的理念。因此,最后进入队列的元素将是第一个退出的。这种方法主要应用于计算器中。
让我们来看看一个堆栈的例子。
using System;
using System.Collections;
class MainClass
{
public static void Main(string[] args)
{
Stack myStk = new Stack(); // Creating a stack
// Adding Items to the top most of the stack
myStk.Push(1); //The Push() method is used to add items to the top most of the stack
myStk.Push(4);
myStk.Push(3);
myStk.Push(6);
myStk.Push(4);
Console.WriteLine(myStk.Pop()); // The pop() method removes the top item from the stack and returns it.
Console.WriteLine(myStk.Peek()); //The Peek() method just returns the items at the top of the list without deleting them.
Console.WriteLine(myStk.Peek());
Console.WriteLine();
foreach (int i in myStk)
{
Console.WriteLine(i);
}
}
}
运行上面的片段将显示如下。
4
6
6
6
3
4
1
队列
Queue 是一个很好的例子集合,它反映了 "先入先出 "的思想。因此,第一个进入队列的元素将是第一个退出的。
让我们看看一个队列的例子。
using System;
using System.Collections;
class MainClass
{
public static void Main(string[] args)
{
Queue animals = new Queue();
animals.Enqueue("Animal 3"); //adds items at the end of the queue
animals.Enqueue("Animal 4");
animals.Enqueue("Animal 5");
Console.WriteLine(animals.Dequeue()); // removes the top item from the queue and returns it.
Console.WriteLine(animals.Peek());
Console.WriteLine(animals.Peek());
Console.WriteLine();
foreach (string anml in animals)
{
Console.WriteLine(anml);
}
}
}
运行上面的片段将显示如下。
Animal 3
Animal 4
Animal 4
Animal 4
Animal 5
Hashtable
Hashtable 类似于ArrayList ,它以键值对的形式存储事物。
让我们看一个Hashtable的例子。
using System;
using System.Collections;
class MainClass
{
public static void Main(string[] args)
{
Hashtable ht = new Hashtable(); //Creating a Hashtable
// Adding elements in the Hashtable
ht.Add("msg", "Message 1");
ht.Add("car", "BMW");
ht.Add("ct", "Cat");
ht.Add("asp", "asp.net");
foreach (DictionaryEntry d in ht)
{
Console.WriteLine(d.Key + " " + d.Value);
}
}
}
运行上面的片段将显示如下。
car BMW
ct Cat
msg Message 1
asp asp.net
在这种情况下,输出的顺序并不重要。
总结
在这篇文章中,我们已经了解了C#中常见集合的基础知识。集合有助于管理数据的集合。