在c#中,集合是用于存储和操作一组数据项的数据结构。这些集合通常位于 system.collections
和 system.collections.generic
命名空间中。下面我将概述c#中几种常用的集合类型及其特点:
1. system.collections 命名空间中的集合
这个命名空间中的集合类型不支持泛型,因此在编译时不检查类型安全性。这意味着在运行时可能会遇到类型转换错误。
arraylist
- 动态数组,可以存储任意类型的对象。
- 缺乏类型安全性。
- 提供了
add
,insert
,remove
,sort
,reverse
等方法。
示例:
arraylist list = new arraylist(); list.add(1); list.add("two");
hashtable
- 键值对集合,键必须是
object
类型。 - 键必须唯一。
- 缺乏类型安全性。
- 提供了
add
,remove
,containskey
,containsvalue
等方法。
- 键值对集合,键必须是
示例:
hashtable table = new hashtable(); table.add("key", "value");
stack
- 后进先出 (lifo) 集合。
- 支持
push
和pop
方法。
示例:
stack<object> stack = new stack<object>(); stack.push(1); stack.push("two"); object top = stack.pop(); // "two"
queue
- 先进先出 (fifo) 集合。
- 支持
enqueue
和dequeue
方法。
示例:
queue<object> queue = new queue<object>(); queue.enqueue(1); queue.enqueue("two"); object front = queue.dequeue(); // 1
2. system.collections.generic 命名空间中的集合
这个命名空间中的集合类型支持泛型,因此可以确保类型安全性。
list
- 动态数组,可以存储特定类型的对象。
- 提供了
add
,insert
,remove
,sort
,reverse
等方法。
示例:
list<int> numbers = new list<int>(); numbers.add(1); numbers.add(2);
hashset
- 用于存储唯一元素的集合。
- 提供了
add
,remove
,contains
等方法。
示例:
var hashset = new hashset<string>(); hashset.add("a"); hashset.add("c"); hashset.add("b"); hashset.add("a"); hashset.add("c"); hashset.add("b"); foreach (var item in hashset) { console.writeline(item); } /*输出结果 a b c */
dictionary<tkey, tvalue>
- 键值对集合,键和值都可以是特定类型。
- 键必须唯一。
- 提供了
add
,remove
,trygetvalue
,containskey
等方法。
示例:
dictionary<string, int> scores = new dictionary<string, int>(); scores.add("alice", 90); scores.add("bob", 80);
sorteddictionary<tkey, tvalue>
- 键值对集合,按照键排序。
- 键必须唯一。
- 提供了
add
,remove
,trygetvalue
,containskey
等方法。
示例:
var sortdic = new sorteddictionary<int, string>(); sortdic.add(10, "十"); sortdic.add(5, "五"); sortdic.add(1, "一"); console.writeline(sortdic.keys); foreach (var item in sortdic) { console.writeline($"{item.key}~{item.value}"); } /*输出结果 1~一 5~五 10~十 */
queue
- 泛型的先进先出 (fifo) 集合。
- 支持
enqueue
和dequeue
方法。
示例:
var queue = new queue<int>(); queue.enqueue(1); queue.enqueue(2); queue.enqueue(3); foreach (var item in queue) { console.writeline(item); } console.writeline($"dequeue元素:{queue.dequeue()}"); /*输出结果 1 2 3 dequeue元素:1 */
stack
- 泛型的后进先出 (lifo) 集合。
- 支持
push
和pop
方法。
示例:
var stack = new stack<int>(); stack.push(1); stack.push(2); stack.push(3); foreach (var item in stack) { console.writeline(item); } //pop元素 console.writeline($"pop元素:{stack.pop()}"); /*输出结果 3 2 1 pop元素:3 */
linkedlist
- 双向链表,适合频繁插入和删除的场景。
- 支持
addfirst
,addlast
,removefirst
,removelast
等方法。
示例:
var linkedlist = new linkedlist<string>(); linkedlist.addlast("2"); linkedlist.addlast("3"); linkedlist.addlast("5"); linkedlist.addfirst("1"); linkedlist.addbefore(linkedlist.find("5"), "4"); foreach (var item in linkedlist) { console.writeline(item); } console.writeline($"2前面的值:{linkedlist.find("2").previous.value}"); console.writeline($"2后面的值:{linkedlist.find("2").next.value}"); /*输出结果 1 2 3 4 5 2前面的值:1 2后面的值:3 */
到此这篇关于概述c#中各种类型集合的特点的文章就介绍到这了,更多相关概述c#中各种类型集合的特点内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论