C# 中的字典方法
字典是 C# 中的一组键和值。Dictionary<TKey, TValue> 包含在 System.Collection.Generics 命名空间中。
以下为方法 −
| 序数 | 方法及说明 |
|---|---|
| 1 | Add 在字典中添加键值对 |
| 2 | Clear() 移除所有键和值 |
| 3 | Remove 移除指定键的元素。 |
| 4 | ContainsKey 检查指定的键是否存在于 Dictionary<TKey, TValue> 中。 |
| 5 | ContainsValue 检查指定的键值是否存在于 Dictionary<TKey, TValue> 中。 |
| 6 | Count 计算键值对的数量。 |
| 7 | Clear 从 Dictionary<TKey, TValue> 中移除所有元素。 |
让我们了解如何在字典中添加元素并显示数量。
实例
using System;
using System.Collections.Generic;
public class Demo {
public static void Main() {
IDictionary<int, int> d = new Dictionary<int, int>();
d.Add(1,97);
d.Add(2,89);
d.Add(3,77);
d.Add(4,88);
d.Add(5,78);
d.Add(6,98);
Console.WriteLine(d.Count);
}
}输出
6
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP