Skip to content

Instantly share code, notes, and snippets.

@kamiyaowl
Created March 16, 2014 08:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kamiyaowl/9580404 to your computer and use it in GitHub Desktop.
Save kamiyaowl/9580404 to your computer and use it in GitHub Desktop.
c#でList<Tuple2`>をDictionaryに変換
(0, Hoge)
(1, Foo)
(2, Bar)
[0, Hoge]
[1, Foo]
[2, Bar]
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