[C#]Dictionary(連想配列)の要素数を取得するには?

C#

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

Dictionary(連想配列)の要素の個数を取得する方法を紹介します。

スポンサーリンク

方法

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

Dictionary(連想配列)の要素数を取得するには、Countプロパティを使います。

具体的には、DictionaryのCountプロパティにアクセスします

int itemCount = dict.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 },
	    };
	    
	    int itemCount = numbers.Count;
	    
	    Console.WriteLine(itemCount); //5
	}
}

コメント

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