Last active
January 27, 2020 02:21
-
-
Save divide-by-zero/060b7f95e2c66e2ae46f94b0ecea7ae4 to your computer and use it in GitHub Desktop.
keyが存在しない場合はデフォルト値を返すDictionaryの拡張 ref: https://qiita.com/divideby_zero/items/cd6f0c901a434bc891be
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var dict = new Dictionary<string,string>(); | |
| var t = dict["test"];//↑で作ったばかりなので入ってるはずもなく、KeyNotFoundException |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public static class DictionaryExtensions { | |
| /// <summary> | |
| /// 値を取得、keyがなければデフォルト値を設定し、デフォルト値を取得 | |
| /// </summary> | |
| public static TV GetOrDefault<TK, TV>(this Dictionary<TK, TV> dic, TK key,TV defaultValue = default(TV)) | |
| { | |
| TV result; | |
| return dic.TryGetValue(key, out result) ? result : defaultValue; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var dict = new Dictionary<string,string>(); | |
| //var t = dict["test"];//↑で作ったばかりなので入ってるはずもなく、KeyNotFoundException | |
| var t = dict.GetOrDefault("test"); //default値を指定しない場合はdefault(KV),KV=stringなので、stringのデフォ値のnullがtに入る | |
| var t2 = dict.GetOrDefault("test2","入ってなかったよ"); //default値を明示的に指定した場合は、keyが存在しない場合にそれが使用される。 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public static class DictionaryExtensions { | |
| /// <summary> | |
| /// 値を取得、keyがなければデフォルト値を設定し、デフォルト値を取得 | |
| /// </summary> | |
| public static TV GetOrDefault<TK, TV>(this Dictionary<TK, TV> dic, TK key,TV defaultValue = default(TV)) | |
| { | |
| return dic.TryGetValue(key, out var result) ? result : defaultValue; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public static class DictionaryExtensions { | |
| /// <summary> | |
| /// 値を取得、keyがなければデフォルト値を設定し、デフォルト値を取得 | |
| /// </summary> | |
| public static TV GetOrDefault<TK, TV>(this Dictionary<TK, TV> dic, TK key,TV defaultValue = default) => dic.TryGetValue(key, out var result) ? result : defaultValue; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment