📌  相关文章
📜  C#|获取StringDictionary中的键/值对的数量

📅  最后修改于: 2021-05-30 01:44:12             🧑  作者: Mango

StringDictionary.Count属性用于获取StringDictionary中的键/值对的数量

句法:

public virtual int Count { get; }

返回值:返回StringDictionary中的键/值对的数量。

注意:检索此属性的值是O(1)操作。

下面的程序说明了StringDictionary.Count属性的用法:

范例1:

// C# code to get the number of key/value
// pairs in the StringDictionary
using System;
using System.Collections;
using System.Collections.Specialized;
  
class GFG {
  
    // Driver code
    public static void Main()
    {
  
        // Creating a StringDictionary named myDict
        StringDictionary myDict = new StringDictionary();
  
        // Getting the number of key/value
        // pairs in the StringDictionary
        Console.Write("Number of key/value pairs in myDict are : ");
  
        Console.WriteLine(myDict.Count);
    }
}
输出:
Number of key/value pairs in myDict are : 0

范例2:

// C# code to get the number of key/value
// pairs in the StringDictionary
using System;
using System.Collections;
using System.Collections.Specialized;
  
class GFG {
  
    // Driver code
    public static void Main()
    {
  
        // Creating a StringDictionary named myDict
        StringDictionary myDict = new StringDictionary();
  
        // Adding key and value into the StringDictionary
        myDict.Add("G", "Geeks");
        myDict.Add("F", "For");
        myDict.Add("C", "C++");
        myDict.Add("DS", "Data Structures");
        myDict.Add("N", "Noida");
  
        // Getting the number of key/value
        // pairs in the StringDictionary
        Console.Write("Number of key/value pairs in myDict are : ");
  
        Console.WriteLine(myDict.Count);
    }
}
输出:
Number of key/value pairs in myDict are : 5

参考:

  • https://docs.microsoft.com/zh-cn/dotnet/api/system.collections.specialized.stringdictionary.count?view=netframework-4.7.2