C# 中的 StringDictionary 类?
StringDictionay 类实现了一个哈希表,其键和值都强类型化为字符串,而不是对象。
以下是 StringDictionary 类的属性:
| 序号 | 属性及描述 |
|---|---|
| 1 | Count 获取 StringDictionary 中键值对的数量。 |
| 2 | IsSynchronized 获取一个值,该值指示对 StringDictionary 的访问是否同步(线程安全)。 |
| 3 | tem[String] 获取或设置与指定键关联的值。 |
| 4 | Keys 获取 StringDictionary 中键的集合。 |
| 5 | SyncRoot 获取一个对象,该对象可用于同步对 StringDictionary 的访问。 |
| 6 | Values 获取 StringDictionary 中值的集合。 |
以下是 StringDictionary 类的一些方法:
| 序号 | 方法及描述 |
|---|---|
| 1 | Add(String, String) 将具有指定键和值的条目添加到 StringDictionary 中。 |
| 2 | Clear() 从 StringDictionary 中移除所有条目。 |
| 3 | ContainsKey(String) 确定 StringDictionary 是否包含特定的键。 |
| 4 | ContainsValue(String) 确定 StringDictionary 是否包含特定的值。 |
| 5 | CopyTo(Array, Int32) 将字符串字典值复制到指定索引处的一维数组实例中。 |
| 6 | Equals(Object) 确定指定对象是否等于当前对象。(从 Object 继承) |
示例
现在让我们看一些示例:
要检查两个 StringDictionary 对象是否相等,代码如下:
using System;
using System.Collections.Specialized;
public class Demo {
public static void Main() {
StringDictionary strDict1 = new StringDictionary();
strDict1.Add("A", "John");
strDict1.Add("B", "Andy");
strDict1.Add("C", "Tim");
strDict1.Add("D", "Ryan");
strDict1.Add("E", "Kevin");
strDict1.Add("F", "Katie");
strDict1.Add("G", "Brad");
StringDictionary strDict2 = new StringDictionary();
strDict2.Add("A", "John");
strDict2.Add("B", "Andy");
strDict2.Add("C", "Tim");
strDict2.Add("D", "Ryan");
strDict2.Add("E", "Kevin");
strDict2.Add("F", "Katie");
strDict2.Add("G", "Brad");
StringDictionary strDict3 = new StringDictionary();
strDict3 = strDict2;
Console.WriteLine("Is Dictionary2 equal to Dictionary3? = "+strDict2.Equals(strDict3));
Console.WriteLine("Is Dictionary1 equal to Dictionary3? = "+strDict1.Equals(strDict3));
}
}输出
这将产生以下输出:
Is Dictionary2 equal to Dictionary3? = True Is Dictionary1 equal to Dictionary3? = False
示例
要检查 StringDictionary 是否同步,代码如下:
using System;
using System.Collections;
using System.Collections.Specialized;
public class Demo {
public static void Main() {
StringDictionary strDict1 = new StringDictionary();
strDict1.Add("A", "John");
strDict1.Add("B", "Andy");
strDict1.Add("C", "Tim");
strDict1.Add("D", "Ryan");
strDict1.Add("E", "Kevin");
strDict1.Add("F", "Katie");
strDict1.Add("G", "Brad");
Console.WriteLine("StringDictionary1 elements...");
foreach(DictionaryEntry de in strDict1) {
Console.WriteLine(de.Key + " " + de.Value);
}
StringDictionary strDict2 = new StringDictionary();
strDict2.Add("1", "A");
strDict2.Add("2", "B");
strDict2.Add("3", "C");
strDict2.Add("4", "D");
strDict2.Add("5", "E");
Console.WriteLine("
StringDictionary2 key-value pairs...");
IEnumerator demoEnum = strDict2.GetEnumerator();
DictionaryEntry d;
while (demoEnum.MoveNext()) {
d = (DictionaryEntry)demoEnum.Current;
Console.WriteLine("Key = " + d.Key + ", Value = " + d.Value);
}
Console.WriteLine("Is the StringDictionary2 synchronized? = "+strDict2.IsSynchronized);
}
}输出
这将产生以下输出:
StringDictionary1 elements... a John b Andy c Tim d Ryan e Kevin f Katie g Brad StringDictionary2 key-value pairs... Key = 1, Value = A Key = 2, Value = B Key = 3, Value = C Key = 4, Value = D Key = 5, Value = E Is the StringDictionary2 synchronized? = False
广告
数据结构
网络
关系型数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP