Skip to content

Instantly share code, notes, and snippets.

@mariusGundersen
Created October 21, 2018 13:04
Show Gist options
  • Save mariusGundersen/35fa20bd963dd9e80145da0266d52988 to your computer and use it in GitHub Desktop.
Save mariusGundersen/35fa20bd963dd9e80145da0266d52988 to your computer and use it in GitHub Desktop.
Duct-typed extension methods Example 11
// Example 11: add with params
// Use params to take several ints, each representing 3 digits
public static void Add<TKey>(this IDictionary<TKey, int> dictionary, TKey key, params int[] values)
=> dictionary.Add(key, values.Aggregate(0, (s, v) => s*1000 + v));
// Now we can write it like this
var populations = new Dictionary<string, int>
{
{ "China", 1,409,517,397 },
{ "India", 1,339,180,127 },
{ "USA", 324,459,463 },
{ "Indonesia", 263,991,379 },
{ "Brazil", 209,288,278 }
};
// Output each item, to see if things work correctly
foreach(var (key, value) in populations)
{
Console.WriteLine($"{key}: {value}");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment