当前位置: 首页 > news >正文

C#集合及其操作

在C#中,集合是一种用于存储和管理多个对象的数据结构。.NET框架提供了丰富的集合类型,以满足不同的编程需求。以下是一些常见集合类型的详细介绍及其操作演示:

1. 列表(List<T>

  • 介绍List<T> 是一个泛型集合,它可以动态调整大小,方便地添加、删除和访问元素。它在 System.Collections.Generic 命名空间中。
  • 操作演示
using System;
using System.Collections.Generic;class Program
{static void Main(){// 创建一个整数列表List<int> numbers = new List<int>();// 添加元素numbers.Add(1);numbers.Add(2);numbers.Add(3);// 插入元素numbers.Insert(1, 10); // 在索引1处插入10// 访问元素int firstNumber = numbers[0]; // 获取第一个元素// 修改元素numbers[2] = 20;// 删除元素numbers.Remove(20); // 删除值为20的元素numbers.RemoveAt(1); // 删除索引1处的元素// 遍历列表foreach (int number in numbers){Console.WriteLine(number);}// 列表大小int count = numbers.Count;}
}

2. 数组列表(ArrayList

  • 介绍ArrayList 是一个非泛型集合,可以存储不同类型的对象,但由于类型安全问题,在现代C#编程中,List<T> 更常用。它在 System.Collections 命名空间中。
  • 操作演示
using System;
using System.Collections;class Program
{static void Main(){ArrayList arrayList = new ArrayList();// 添加元素arrayList.Add(1);arrayList.Add("Hello");arrayList.Add(true);// 访问元素int firstElement = (int)arrayList[0]; // 需要进行类型转换// 删除元素arrayList.Remove("Hello");// 遍历列表foreach (object item in arrayList){Console.WriteLine(item);}// 列表大小int count = arrayList.Count;}
}

3. 字典(Dictionary<TKey, TValue>

  • 介绍Dictionary<TKey, TValue> 是一个泛型集合,用于存储键值对。它提供了快速的查找功能,基于键来访问值。它在 System.Collections.Generic 命名空间中。
  • 操作演示
using System;
using System.Collections.Generic;class Program
{static void Main(){// 创建一个字符串到整数的字典Dictionary<string, int> scores = new Dictionary<string, int>();// 添加键值对scores.Add("Alice", 85);scores.Add("Bob", 90);// 访问值int aliceScore = scores["Alice"];// 修改值scores["Bob"] = 95;// 检查键是否存在bool hasKey = scores.ContainsKey("Charlie");// 删除键值对scores.Remove("Bob");// 遍历字典foreach (KeyValuePair<string, int> pair in scores){Console.WriteLine($"{pair.Key}: {pair.Value}");}}
}

4. 哈希集(HashSet<T>

  • 介绍HashSet<T> 是一个泛型集合,它存储唯一的元素,不允许重复。它在 System.Collections.Generic 命名空间中。
  • 操作演示
using System;
using System.Collections.Generic;class Program
{static void Main(){// 创建一个整数哈希集HashSet<int> numbers = new HashSet<int>();// 添加元素numbers.Add(1);numbers.Add(2);numbers.Add(2); // 重复添加,不会增加新元素// 检查元素是否存在bool contains = numbers.Contains(1);// 删除元素numbers.Remove(2);// 遍历哈希集foreach (int number in numbers){Console.WriteLine(number);}}
}

5. 队列(Queue<T>

  • 介绍Queue<T> 是一个泛型集合,它遵循先进先出(FIFO)的原则。新元素添加到队列的末尾,元素从队列的开头移除。它在 System.Collections.Generic 命名空间中。
  • 操作演示
using System;
using System.Collections.Generic;class Program
{static void Main(){// 创建一个整数队列Queue<int> queue = new Queue<int>();// 入队queue.Enqueue(1);queue.Enqueue(2);queue.Enqueue(3);// 出队int first = queue.Dequeue(); // 返回1// 查看队首元素但不移除int peek = queue.Peek(); // 返回2// 队列大小int count = queue.Count;}
}

6. 栈(Stack<T>

  • 介绍Stack<T> 是一个泛型集合,它遵循后进先出(LIFO)的原则。新元素添加到栈的顶部,元素从栈的顶部移除。它在 System.Collections.Generic 命名空间中。
  • 操作演示
using System;
using System.Collections.Generic;class Program
{static void Main(){// 创建一个整数栈Stack<int> stack = new Stack<int>();// 入栈stack.Push(1);stack.Push(2);stack.Push(3);// 出栈int top = stack.Pop(); // 返回3// 查看栈顶元素但不移除int peek = stack.Peek(); // 返回2// 栈大小int count = stack.Count;}
}

这些集合类型在C#编程中非常常用,根据具体的需求选择合适的集合类型可以提高程序的效率和可读性。

http://www.jsqmd.com/news/56409/

相关文章:

  • Windows和Office激活工具
  • 软件基础课程第三次作业
  • IL2CPP逆向
  • 程序员修炼之道:从小工到专家读后感2
  • ubuntu学习笔记1.文件权限
  • 20232424 2025-2026-1 《网络与系统攻防技术》实验七实验报告
  • peak物品生成列表对照
  • EverEdit 提供了强大的函数提示功能
  • NOIP2025游寄
  • 代码大全2(4)
  • 代码大全2(5)
  • 代码大全2(6)
  • FastAPI(TortoiseORM+Aerich)和Flask(sqlalchemy+Migrate)数据库持久化
  • 2025年长春笔记本电脑售后维修点推荐:联想华硕戴尔等品牌哪家更靠谱?全方位评测与用户口碑解析
  • 2025年南京笔记本电脑售后维修点推荐:华硕惠普宏碁等品牌哪家更可靠?行业数据与服务质量比对
  • 为什么硬盘的容量宣传与实际不一致?
  • 2025年南京笔记本电脑售后维修点推荐:哪个性价比最高?多品牌维修点对比与选购指南
  • 【ABC135F】Strings of Eternity
  • 2025年南通笔记本售后点推荐:三星戴尔联想等品牌哪家性价比最高?深度解析与选择对比
  • C++ 非模板的右值引用
  • 2025年南通笔记本电脑售后维修点推荐:哪个维修点更靠谱?七大品牌实测对比与选择指南
  • 2025年北京笔记本电脑售后维修点推荐:联想戴尔惠普等品牌服务如何选择?多维度对比与排名指南
  • 2025年大连笔记本售后服务点推荐:三星戴尔联想等品牌哪家更可靠?全面评测与用户反馈解析
  • 2025年重庆笔记本电脑售后维修点推荐:联想华硕戴尔等品牌哪家强?多维度实测与用户口碑解析
  • 2025年郑州笔记本电脑维修点推荐:联想华硕戴尔等品牌哪家服务更优?全方位评测与口碑分析
  • 2025年重庆惠普宏碁三星维修点推荐:哪家维修质量更优?多维度实测与用户口碑调查
  • 2025年郑州笔记本电脑售后维修点推荐:惠普宏碁三星等品牌维修服务如何选?多维度对比与排名指南
  • 2025年贵阳笔记本电脑售后维修点推荐:哪个技术更可靠?多品牌维修能力全面评测
  • 20232326 2025-2026-1 《网络与系统攻防技术》实验七实验报告
  • 20232412 2025-2026-1 《网络与系统攻防技术》实验七实验报告