集合类型

在 C# 中,有许多内置的集合数据类型可用于存储和操作数据。以下是一些常见的集合数据类型及其简单用法:

一、List

在C#中,List是一个强大的动态数组,它可以根据需要动态扩展或缩小,可以动态添加、删除元素。

下面是一些关于List的常见用法:

1. 创建List对象:

usingusingSystem;
System.Collections.Generic;
class Program
{
	static void Main()
	{
		// 创建一个整数类型的List
		List<int> intList = new List<int>();
		// 添加元素到List
		intList.Add(3);
		intList.Add(5);
		intList.Add(7);

		// 将元素插入到List指定位置
		intList.Insert(1, 4);
		// 从List中移除元素
		intList.Remove(5);
		// 清空List
		intList.Clear();
	}
}

2. 访问List元素:

// 获取List中的元素个数
int count = intList.Count;
// 通过索引访问List中的元素
int element = intList[2];

3. 遍历List元素:

// 遍历List中的元素
foreach (int num in intList)
{
	Console.WriteLine(num);
}

4.其他方法:

  • Contains:检查List是否包含指定元素。
  • IndexOf :获取指定元素在List中的索引。
  • Sort :对List元素进行排序。
  • ToArray :将List转换为数组。

List在C#中提供了方便的动态数组操作方法,可以方便地进行元素的增加、删除、访问和遍历等操作。由于其动态调整大小的特性,List常被用于需要灵活处理元素数量的场景,如游戏数据管理、集合操作等方面。

二、Queue

在 C# 中,Queue(队列)是一种先进先出(FIFO)的数据结构,用于存储一组对象。

以下是一些关于Queue 的常见用法。

1. 创建 Queue 对象:

usingusingSystem;
System.Collections;
class Program
{
	static void Main()
	{
		// 创建一个整数类型的	Queue
		Queue myQueue = new Queue();


		// 向Queue中添加元素
		myQueue.Enqueue(3);
		myQueue.Enqueue(5);
		myQueue.Enqueue(7);
	}
}

2. 从 Queue 中移除元素并访问元素:

 

// 从Queue中移除并返回位于队列开始处的对象
object obj = myQueue.Dequeue();
// 获取但不移除位于队列开始处的对象
object firstElement = myQueue.Peek();

3. 遍历 Queue 中的元素:

// 遍历Queue中的元素
foreach (int num in myQueue)
{
	Console.WriteLine(num);
}

4. 其他方法:

  • Count :获取 Queue 中的元素个数。
  • Clear :清空 Queue 中的所有元素。
  • Contains :检查指定元素是否在 Queue 中。
     

Queue 是一种常用的数据结构,特别适合在需要按照先进先出原则进行操作的场景,例如处理任务队列、消息传递等。在游戏开发中,Queue 可以用于处理游戏中的事件队列、动作序列等需求。

三、Stack

在 C# 中,Stack(栈)是一种后进先出(LIFO)的数据结构,用于存储一组对象。

以下是一些关于Stack 的常见用法:

1. 创建 Stack 对象:


usingusingSystem;
System.Collections;
class Program
{
	static void Main()
	{
		// 创建一个整数类型的Stack
		Stack myStack = new Stack();
		// 向Stack中添加元素
		myStack.Push(3);
		myStack.Push(5);
		myStack.Push(7);
	}
}	

2. 从 Stack 中移除元素并访问元素:

// 从Stack中移除并返回位于栈顶的对象
object obj = myStack.Pop();
// 获取但不移除位于栈顶的对象
object topElement = myStack.Peek();

3. 遍历 Stack 中的元素:

// 遍历Stack中的元素
foreach (int num in myStack)
{
	Console.WriteLine(num);
}

4. 其他方法:

  • Count :获取 Stack 中的元素个数。
  • Clear :清空 Stack 中的所有元素。
  • Contains :检查指定元素是否在 Stack 中。

Stack 是一种常用的数据结构,特别适合在需要按照后进先出原则进行操作的场景,例如处理表达式求值、函数调用、历史记录等。在游戏开发中,Stack 可以用于处理游戏中的状态切换、撤销操作、资源加载等需求。 

四、HashSet

在 C# 中,HashSet 是一种集合类型,用于存储不重复的元素集合。

以下是一些关于 HashSet 的常见用法:

1. 创建 HashSet 对象:

using System;
using System.Collections.Generic;
class Program
{
	static void Main()
	{
		// 创建一个整数类型的 HashSet
		HashSet<int> myHashSet = new HashSet<int>();
		// 向 HashSet 中添加元素
		myHashSet.Add(3);
		myHashSet.Add(5);
		myHashSet.Add(7);
	}
}

2. 访问和修改 HashSet 中的元素:

// 判断元素是否在 HashSet 中
bool contains = myHashSet.Contains(5);
// 从 HashSet 中移除元素
myHashSet.Remove(5);

3. 遍历 HashSet 中的元素:

// 遍历 HashSet 中的元素
foreach (int num in myHashSet)
{
	Console.WriteLine(num);
}

4. 其他方法:

  • Count :获取 HashSet 中的元素个数。
  • Clear :清空 HashSet 中的所有元素。
  • UnionWith :将当前 HashSet 与指定集合的并集合并。
  • IntersectWith :修改当前 HashSet 以仅包含其和指定集合中的相同元素。
  • IsSubsetOf :确定当前 HashSet 是否为另一个集合的子集。

HashSet 是一种非常有用的数据结构,它提供了高效的元素查找和去重能力。在游戏开发中,HashSet
可以用于处理需要快速查找和去重的元素集合,例如游戏中的物体标记、碰撞检测、事件订阅等场景。

五、Dictionary<TKey, TValue>

在 C# 中,Dictionary 是一种键值对集合,它允许你存储一组键值对,并且通过键来快速检索对应的数值。以下是一些关于 Dictionary 的常见用法:

1. 创建 Dictionary 对象:

usingusingSystem;
System.Collections.Generic;
class Program
{
	static void Main()
	{
		// 创建一个字符串类型为键,整数类型为值的Dictionary
		Dictionary<string, int> myDict = new Dictionary<string, int>();

		// 添加键值对到Dictionary
		myDict.Add("apple", 10);
		myDict.Add("banana", 20);
		myDict.Add("orange", 15);
	}
}

2. 访问和修改 Dictionary 中的元素:

// 通过键访问Dictionary中的值
int value = myDict["apple"];
// 修改特定键的值
myDict["banana"] = 25;
// 移除指定键值对
myDict.Remove("orange");

3. 遍历 Dictionary 中的元素:

// 遍历Dictionary中的键值对
foreach (var pair in myDict)
{
	Console.WriteLine("Key: {0}, Value: {1}", pair.Key, pair.Value);
}

4. 其他方法:

  • ContainsKey :检查Dictionary是否包含指定的键。
  • ContainsValue :检查Dictionary是否包含指定的值。
  • TryGetValue :尝试获取与指定键相关联的值。

Dictionary是用于存储键值对的一种非常有用的数据结构,它提供了快速的键值查找和修改能力。在游戏
开发中,它常用于管理游戏中的键值数据,例如存储玩家属性、道具信息、配置数据等。

“您的支持是我持续分享的动力”

微信收款码
微信
支付宝收款码
支付宝

目录