人生が変わるオンラインサロン!
月3万副業収入が得られる!Mサロン!31日間無料体験!今回のテーマ
こんばんは、サルモリです。
今回は下記のテーマについての記事を書いていこうと思います。
今回のテーマ
Dictionaryの使い方について解説2
前回の記事では紹介しきれていないDictionaryの使い方を学んでいきます。前回の記事は下記の記事です。
CHECK
-
-
C# Dictionaryの使い方を覚えよう ディクショナリ型でのLinqの使い方も解説
人生が変わるオンラインサロン! 月3万副業収入が得られる!Mサロン!31日間無料体験! 今回のテーマ こんばんは、サルモリです。 今回は下記のテーマについての記事を書いていこうと思います。 今回のテー ...
続きを見る

他のLinqの記事についてはこちら
配列、リストなどの記事はこちら
使用するクラス
使い方を紹介する上でクラスの定義が必要なので、今回は下記のクラスを使用します。何回か使用したことがあるロボットクラスです。
使用するクラスの定義
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
class Robot { string name, color, maker; int strength; public Robot(string _name, string _color, int _strength, string _maker) { name = _name; color = _color; strength = _strength; maker = _maker; } public String Name { get { return name; } } public String Color { get { return color; } } public int Strength { get { return strength; } } public String Maker { get { return maker; } } } |
Dictionaryに定義したクラスのオブジェクトを格納する
早速、上記のクラスをDictionaryに格納してみましょう。
1 2 3 4 5 6 7 8 9 |
var RobotDict = new Dictionary<string, Robot>() { ["0001"] = new Robot("Rockman","Blue",5, "Dr Light"), ["0002"] = new Robot("Roll", "Pink", 1, "Dr Light"), ["0003"] = new Robot("Airman", "Blue", 3, "Dr Wily"), ["0004"] = new Robot("Quickman", "Red", 5, "Dr Wily"), ["0005"] = new Robot("Iceman", "Blue", 4, "Dr Wily"), ["0006"] = new Robot("FIreman", "Red", 4, "Dr Wily"), }; |
今回はKeyをロボットコードとして、要素をロボットオブジェクトとして生成し、格納してみました。
Dictionaryからクラスで定義したオブジェクトを取り出す方法
クラスで定義したオブジェクトを取り出してみましょう。
クラスで定義したロボット名を出力
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
using System; using System.Collections.Generic; using System.Linq; namespace Dictionary { class Program { static void Main(string[] args) { var RobotDict = new Dictionary<string, Robot>() { ["0001"] = new Robot("Rockman","Blue",5, "Dr Light"), ["0002"] = new Robot("Roll", "Pink", 1, "Dr Light"), ["0003"] = new Robot("Airman", "Blue", 3, "Dr Wily"), ["0004"] = new Robot("Quickman", "Red", 5, "Dr Wily"), ["0005"] = new Robot("Iceman", "Blue", 4, "Dr Wily"), ["0006"] = new Robot("FIreman", "Red", 4, "Dr Wily"), }; foreach(var robot in RobotDict) { Console.WriteLine(robot.Key + ":" + robot.Value.Name); } } } } |
result
0001:Rockman
0002:Roll
0003:Airman
0004:Quickman
0005:Iceman
0006:FIreman
今回の例では、キーの値と要素の中のロボット名を出力しています。
DictionaryでLinqを使用した例 OrderbyとGroupby
クラスで定義したオブジェクトに対して、Linqを使用してみましょう。
やりたい処理
強さ順に並べ替えたDictionaryを作成したい。
クラスで定義したロボット名を出力
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
using System; using System.Collections.Generic; using System.Linq; namespace Dictionary { class Program { static void Main(string[] args) { var RobotDict = new Dictionary<string, Robot>() { ["0001"] = new Robot("Rockman","Blue",5, "Dr Light"), ["0002"] = new Robot("Roll", "Pink", 1, "Dr Light"), ["0003"] = new Robot("Airman", "Blue", 3, "Dr Wily"), ["0004"] = new Robot("Quickman", "Red", 5, "Dr Wily"), ["0005"] = new Robot("Iceman", "Blue", 4, "Dr Wily"), ["0006"] = new Robot("FIreman", "Red", 4, "Dr Wily"), }; RobotDict = RobotDict.OrderByDescending(x => x.Value.Strength).ToDictionary(robot => robot.Key, robot => robot.Value); foreach(var robot in RobotDict) { Console.WriteLine(robot.Key + ":" + robot.Value.Name + ":" + robot.Value.Strength); } } } } |
result
0001:Rockman:5
0004:Quickman:5
0005:Iceman:4
0006:FIreman:4
0003:Airman:3
0002:Roll:1
強さ順に並べ替えができました。
もう一つ、色でグループ分けしたDictionaryを作成してみましょう。
やりたい処理
色でグループ分けしたDictionaryを作成したい。
クラスで定義したロボット名を出力
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
using System; using System.Collections.Generic; using System.Linq; namespace Dictionary { class Program { static void Main(string[] args) { var RobotDict = new Dictionary<string, Robot>() { ["0001"] = new Robot("Rockman","Blue",5, "Dr Light"), ["0002"] = new Robot("Roll", "Pink", 1, "Dr Light"), ["0003"] = new Robot("Airman", "Blue", 3, "Dr Wily"), ["0004"] = new Robot("Quickman", "Red", 5, "Dr Wily"), ["0005"] = new Robot("Iceman", "Blue", 4, "Dr Wily"), ["0006"] = new Robot("FIreman", "Red", 4, "Dr Wily"), }; var RobotDictList = RobotDict.GroupBy(x => x.Value.Color); foreach(var robotdict in RobotDictList) { foreach(var robot in robotdict) { Console.WriteLine(robot.Key + ":" + robot.Value.Name + ":" + robot.Value.Color); } Console.WriteLine("------------------------"); } } } } |
result
0001:Rockman:Blue
0003:Airman:Blue
0005:Iceman:Blue
------------------------
0002:Roll:Pink
------------------------
0004:Quickman:Red
0006:FIreman:Red
------------------------
色でグループ分けをすることができましたね。
配列、リストなどの記事はこちら
Linqを使用してリストからDictionary型への変換
最後にリストからDictionaryへの変換について解説します。
まず、下記のようにロボットのリストを作ってみました。
ロボットリスト
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
using System; using System.Collections.Generic; using System.Linq; namespace Dictionary { class Program { static void Main(string[] args) { var robotlist = new List<Robot>(); robotlist.Add(new Robot("Rockman", "Blue", 5, "Dr Light")); robotlist.Add(new Robot("Roll", "Pink", 1, "Dr Light")); robotlist.Add(new Robot("Airman", "Blue", 3, "Dr Wily")); robotlist.Add(new Robot("Quickman", "Red", 5, "Dr Wily")); robotlist.Add(new Robot("Iceman", "Blue", 4, "Dr Wily")); robotlist.Add(new Robot("Fireman", "Red", 4, "Dr Wily")); foreach(var robot in robotlist){ Console.WriteLine(robot.Name); } } } } |
result
Rockman
Roll
Airman
Quickman
Iceman
Fireman
上記のリストをLinqを使用してDictionary型に変換してみます。
ロボットリストをロボットDictionaryに変換
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
using System; using System.Collections.Generic; using System.Linq; namespace Dictionary { class Program { static void Main(string[] args) { var robotlist = new List<Robot>(); robotlist.Add(new Robot("Rockman", "Blue", 5, "Dr Light")); robotlist.Add(new Robot("Roll", "Pink", 1, "Dr Light")); robotlist.Add(new Robot("Airman", "Blue", 3, "Dr Wily")); robotlist.Add(new Robot("Quickman", "Red", 5, "Dr Wily")); robotlist.Add(new Robot("Iceman", "Blue", 4, "Dr Wily")); robotlist.Add(new Robot("Fireman", "Red", 4, "Dr Wily")); var robotDict = robotlist.ToDictionary(robot => robot.Name); Console.WriteLine("Icemancolor = " + robotDict["Iceman"].Color); Console.WriteLine("-------------------------------"); foreach(var robot in robotDict){ Console.WriteLine(robot.Key + ":" + robot.Value.Color + ":" + robot.Value.Maker); } } } } |
result
Icemancolor = Blue
-------------------------------
Rockman:Blue:Dr Light
Roll:Pink:Dr Light
Airman:Blue:Dr Wily
Quickman:Red:Dr Wily
Iceman:Blue:Dr Wily
Fireman:Red:Dr Wily
上記のToDirectory内で何をしているか解説すると、キーをロボット名で定義しています。
なので、Directoryのキーをロボット名で指定すると対応した値を取り出すことが可能です。
下記の疑問についてもお答えします。
そもそもリストをDictionaryにする必要あるの?
仕事でプログラミングをしていた時、使う場面は結構あります。どの場面で使ったかというと、性能改善の時です。
Dictionary型にすると、キーを指定することで一発で要素の取得が出来るのでその特性を活かして、性能改善をすることができます。

まとめ
今回もDictionary型について解説しました。最後のサルモリが言ってることがとても重要です。
開発では、配列、リストをメインに使うことが多いと思いますが、その時の課題に応じて、Dictionaryを使用することも視野にいれましょう。
今回の2記事でDictionaryを使いこなすことが出来ると思うので、何回も見直してぜひ覚えてください。
最後まで見て頂きありがとうございました。

配列、リストなどの記事はこちら
他のLinqの記事についてはこちら
人生が変わるオンラインサロン!
月3万副業収入が得られる!Mサロン!31日間無料体験!C#のLinqをさらに学びたい方へ
C#のLinqを理解して頂くために参考書を執筆致しました!
Linqの演習問題はこちらの参考書に載せているので、問題を解いて理解を深めたい方はこちらの参考書チェックしてください!
下記の画像をクリック!!kindleunlimitedの会員であれば無料で見れます!
⇩初心者向け ⇩中級者向け
C#のLinqをさらに学びたい方へ
C#のLinqを理解して頂くために参考書を執筆致しました!
Linqの演習問題はこちらの参考書に載せているので、問題を解いて理解を深めたい方はこちらの参考書チェックしてください!
下記の画像をクリック!!kindleunlimitedの会員であれば無料で見れます!
⇩初心者向け ⇩中級者向け