[C#]Dictionaryの長さを取得するには?

C#

どうも、ちょげ(@chogetarou)です。

Dictionaryの長さを取得する方法を紹介します。

スポンサーリンク

方法

インターフェース, インターネット, プログラム, ブラウザ, Www

Dictionaryの長さを取得するには、Countプロパティを使います。

具体的には、dict.Countのように、対象のDictionaryのCountプロパティにアクセスします。

myDict.Count

Countプロパティは、アクセス元のDictionaryの長さを取得します。

使用例

using System;
using System.Collections.Generic;
public class Example
{
    public static void Main()
    {
        Dictionary<string, int> numbers = new Dictionary<string, int>()
	    {
	        { "one", 1 },
	        { "two", 2 },
	        { "three", 3 },
	        { "four", 4 },
	        { "five", 5 },
	    };
	    
	    Dictionary<string, int> empty = new Dictionary<string, int>();
	    
	    Console.WriteLine(numbers.Count);
	    Console.WriteLine(empty.Count);
    }
}

/*
    出力:
    5
    0
*/

コメント

タイトルとURLをコピーしました