Created
March 16, 2014 08:59
-
-
Save kamiyaowl/9580404 to your computer and use it in GitHub Desktop.
c#でList<Tuple2`>をDictionaryに変換
This file contains 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
(0, Hoge) | |
(1, Foo) | |
(2, Bar) | |
[0, Hoge] | |
[1, Foo] | |
[2, Bar] |
This file contains 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
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
class TupleToDic { | |
public static void Main() { | |
var src = new List<Tuple<int,String>>(){ | |
new Tuple<int,string>(0,"Hoge"), | |
new Tuple<int,string>(1,"Foo"), | |
new Tuple<int,string>(2,"Bar"), | |
}; | |
foreach(var s in src) { | |
Console.WriteLine(s); | |
} | |
foreach(var kv in src.ToDictionary()) { | |
Console.WriteLine(kv); | |
} | |
} | |
} | |
static class Extension { | |
public static Dictionary<K,V> ToDictionary<K,V>(this IEnumerable<Tuple<K,V>> src) { | |
return src.ToDictionary(t => t.Item1, t => t.Item2); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment