Created
October 21, 2018 13:04
-
-
Save mariusGundersen/35fa20bd963dd9e80145da0266d52988 to your computer and use it in GitHub Desktop.
Duct-typed extension methods Example 11
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
// 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