Adding C# solution.
[IEnumerableExtras.git] / C# / DictionaryToArray.cs
blobf7444250c8974a54fcb589180dc9891e3af356cb
1 using System.Collections.Generic;
3 namespace IEnumerableExtras
5 /// <summary>
6 /// Provedes extension method for <see cref="IDictionary{T,T}"/> to convert dictionary to an array.
7 /// </summary>
8 public static class DictionaryToArray
10 /// <summary>
11 /// Convert this instance of <see cref="IDictionary{T,T}"/> to a rectangular
12 /// arry of type <typeparamref name="T"/>.
13 /// </summary>
14 /// <typeparam name="T">Type for both keys and values of dictionary.</typeparam>
15 /// <param name="dictionary"></param>
16 /// <returns></returns>
17 public static T[,] ToArray< T >( this IDictionary<T, T> dictionary )
19 T[,] result = new T[dictionary.Count,2];
20 int i = 0;
22 foreach ( KeyValuePair<T, T> pair in dictionary )
24 result[ i, 0 ] = pair.Key;
25 result[ i, 1 ] = pair.Value;
27 ++i;
30 return result;