I am not sure how to use an extension method on a dictionary. How do I specify that I need the function applied to the value and not the key?
Here’s a sample code applying the Sum extension method on a list of doubles and a dictionary whose values are doubles. The list extension methods works fine but the dictionary extension method is asking for a selector function.
static void Main(string[] args)
{
List<double> list = new List<double>();
list.Add(34.2);
list.Add(234);
Console.WriteLine(list.Sum());
Dictionary<string, double> dictioanary = new Dictionary<string, double>();
dictioanary.Add("a", 5.34);
dictioanary.Add("b", 44);
Console.WriteLine(dictioanary.Sum());
Console.ReadKey();
}
You have to pass a lambda to the Sum() function: