StringDictionary()构造函数用于初始化StringDictionary类的新实例,该实例将为空,并将具有默认的初始容量。 StringDictionary是一个专门的集合。此类位于System.Collections.Specialized
命名空间下。它仅允许字符串键和字符串值。它遭受性能问题的困扰。它使用键和强类型化为字符串而不是对象的值实现哈希表。
句法:
public StringDictionary ();
范例1:
// C# Program to illustrate how
// to create a StringDictionary
using System;
using System.Collections;
using System.Collections.Specialized;
class Geeks {
// Main Method
public static void Main(String[] args)
{
// sd is the StringDictionary object
// StringDictionary() is the constructor
// used to initializes a new
// instance of the StringDictionary class
StringDictionary sd = new StringDictionary();
// Count property is used to get the
// number of elements in StringDictionary
// It will give 0 as no elements
// are present currently
Console.WriteLine(sd.Count);
}
}
输出:
0
范例2:
// C# Program to illustrate how
// to create a StringDictionary
using System;
using System.Collections;
using System.Collections.Specialized;
class Geeks {
// Main Method
public static void Main(String[] args)
{
// sd is the StringDictionary object
// StringDictionary() is the constructor
// used to initializes a new
// instance of the StringDictionary class
StringDictionary sd = new StringDictionary();
Console.Write("Before Add Method: ");
// Count property is used to get the
// number of elements in StringDictionary
// It will give 0 as no elements
// are present currently
Console.WriteLine(sd.Count);
// Adding key/value pairs in sd
sd.Add("1", "C");
sd.Add("2", "C++");
sd.Add("3", "Java");
sd.Add("4", "Python");
sd.Add("5", "C#");
sd.Add("6", "HTML");
Console.Write("After Add Method: ");
// Count property is used to get the
// number of elements in sd
Console.WriteLine(sd.Count);
}
}
输出:
Before Add Method: 0
After Add Method: 6
参考:
- https://docs.microsoft.com/zh-cn/dotnet/api/system.collections.specialized.stringdictionary.-ctor?view=netframework-4.7.2