在 c# 开发中,集合(collection) 是存储和管理多个对象的重要工具。相比数组,集合更加灵活,可以动态增加、删除元素,并提供丰富的数据操作方法。
本文作为《一个零基础小白的 c# 成长100天》系列教程的一部分,带你从基础理解 c# 集合。
一、什么是 c# 集合?
简单来说:
集合就是一个可以保存多个数据的容器。
例如:数组:
string[] names = new string[3]; names[0] = "张三"; names[1] = "李四"; names[2] = "王五";
问题:
- 数组长度固定
- 添加删除麻烦
- 功能有限
集合:
list<string> names = new list<string>();
names.add("张三");
names.add("李四");
names.add("王五");优势:
✅ 自动扩容
✅ 添加方便
✅ 删除方便
✅ 查询灵活
✅ 支持排序、筛选等操作
二、c# 集合分类
c# 集合主要分为两大类:
集合
│
├── 非泛型集合
│ ├── arraylist
│ ├── hashtable
│
└── 泛型集合
├── list<t>
├── dictionary<tkey,tvalue>
├── hashset<t>
├── queue<t>
└── stack<t>现代 c# 开发中:
推荐优先使用泛型集合。
三、list<t> 集合(最常用)
1. 什么是 list?
list 是动态数组。
命名空间:
using system.collections.generic;
定义:
list<t> 集合名 = new list<t>();
例如:
list<string> students = new list<string>();
students.add("小明");
students.add("小红");
students.add("小刚");2. list 常用方法
添加元素 add()
list<int> nums = new list<int>(); nums.add(10); nums.add(20); nums.add(30);
结果:
10 20 30
批量添加 addrange()
nums.addrange(
new int[]{40,50,60}
);结果:
10 20 30 40 50 60
获取元素
通过索引:
console.writeline(nums[0]);
输出:
10
修改元素
nums[0]=100;
结果:
100 20 30
删除元素 remove()
nums.remove(20);
删除值:
20
根据索引删除 removeat()
nums.removeat(0);
删除第一个元素。
清空集合
nums.clear();
获取数量
console.writeline(nums.count);
四、遍历 list 集合
方式1:for循环
list<string> names =
new list<string>()
{
"张三",
"李四",
"王五"
};
for(int i=0;i<names.count;i++)
{
console.writeline(names[i]);
}方式2:foreach(推荐)
foreach(string name in names)
{
console.writeline(name);
}输出:
张三 李四 王五
五、dictionary<tkey,tvalue> 字典集合
dictionary 是:
键值对集合。
类似:
身份证号码 → 人名 商品编号 → 商品 用户名 → 用户信息
定义:
dictionary<tkey,tvalue>
例如:
dictionary<int,string> users =
new dictionary<int,string>();
users.add(1001,"张三");
users.add(1002,"李四");结构:
1001 ---> 张三 1002 ---> 李四
根据 key 获取 value
console.writeline(users[1001]);
输出:
张三
遍历 dictionary
foreach(var item in users)
{
console.writeline(
item.key + ":" + item.value
);
}输出:
1001:张三 1002:李四
六、hashset<t> 集合
hashset 特点:
不允许重复数据。
例如:
hashset<int> numbers =
new hashset<int>();
numbers.add(10);
numbers.add(20);
numbers.add(10);结果:
10 20
重复的10不会添加。
应用:
- 去重
- 判断是否存在
例如:
if(numbers.contains(20))
{
console.writeline("存在");
}七、queue<t> 队列集合
特点:
先进先出 fifo
生活例子:
排队买票:
第一个进入 ↓ 第一个出去
代码:
queue<string> queue =
new queue<string>();
queue.enqueue("张三");
queue.enqueue("李四");
queue.enqueue("王五");取出:
string name =
queue.dequeue();
console.writeline(name);结果:
张三
八、stack<t> 栈集合
特点:
后进先出 lifo
类似:
一摞书:
第三本 第二本 第一本
拿的时候:
第三本先拿
代码:
stack<int> stack =
new stack<int>();
stack.push(1);
stack.push(2);
stack.push(3);弹出:
console.writeline(
stack.pop()
);结果:
3
九、集合选择指南
| 需求 | 推荐集合 |
|---|---|
| 普通列表 | list<t> |
| 需要键值查询 | dictionary |
| 去重复数据 | hashset |
| 排队处理 | queue |
| 撤销操作 | stack |
| 固定数量数据 | array |
十、集合综合案例:学生管理系统
using system;
using system.collections.generic;
class program
{
static void main()
{
list<string> students =
new list<string>();
while(true)
{
console.writeline(
"1.添加学生 2.查看学生 3.退出");
string choice =
console.readline();
if(choice=="1")
{
console.writeline(
"输入姓名:");
string name =
console.readline();
students.add(name);
}
else if(choice=="2")
{
foreach(var s in students)
{
console.writeline(s);
}
}
else
{
break;
}
}
}
}运行效果:
1.添加学生 2.查看学生 3.退出 输入: 1 姓名: 小明 查看: 小明
以上就是c#中各类集合的基础知识和使用方法详解的详细内容,更多关于c#集合的基础知识和使用方法的资料请关注代码网其它相关文章!
发表评论