在 C# 中创建具有指定初始大小的区分大小写的混合字典


要创建具有指定初始大小的区分大小写的混合字典,代码如下 -

范例

实时演示

using System;
using System.Collections;
using System.Collections.Specialized;
public class Demo {
   public static void Main(){
      HybridDictionary myDict = new HybridDictionary(5, false);
      myDict.Add("A", "AB");
      myDict.Add("B", "BC");
      myDict.Add("C", "DE");
      myDict.Add("D", "FG");
      myDict.Add("e", "fg");
      Console.WriteLine("Key/Value pairs...");
      foreach(DictionaryEntry de in myDict)
      Console.WriteLine("Key = "+de.Key + ", Value = " + de.Value);
   }
}

输出

将产生以下输出 -

Key/Value pairs...
Key = A, Value = AB
Key = B, Value = BC
Key = C, Value = DE
Key = D, Value = FG
Key = e, Value = fg

范例

现在让我们看另一个例子 -

using System;
using System.Collections;
using System.Collections.Specialized;
public class Demo {
   public static void Main(){
      HybridDictionary myDict = new HybridDictionary(5, true);
      myDict.Add("A", "AB");
      myDict.Add("B", "BC");
      myDict.Add("C", "DE");
      myDict.Add("e", "fg");
      myDict.Add("E", "FG");
      Console.WriteLine("key/Value pairs...");
      foreach(DictionaryEntry de in myDict)
      Console.WriteLine("Key = "+de.Key + ", Value = " + de.Value);
   }
}

输出

将产生以下错误 -

Run-time exception : An entry with the same key already exists.
Stack Trace:
[System.ArgumentException: An entry with the same key already exists.]
   at System.Collections.Specialized.ListDictionary.Add(Object key, Object value)
   at System.Collections.Specialized.HybridDictionary.Add(Object key, Object value)
   at Demo.Main() :line 15

更新于: 05-12-2019

71 查看

提升你的职业生涯

通过完成课程获得认证

立即着手
广告
© . All rights reserved.