Skip to content

Instantly share code, notes, and snippets.

@mariusGundersen
mariusGundersen / Example10.cs
Created October 21, 2018 12:55
Duct-typed extension methods Example 10
// Example 10: add a tuple
// This is just a dummy class that we can use in the example
public class City
{
public City(string name, int population, string country)
{
Name = name;
Population = population;
Country = country;
@mariusGundersen
mariusGundersen / Example11.cs
Created October 21, 2018 13:04
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 },