find the sum of all values in a map in csharp

To find the sum of all values in a map in C#, you can iterate through the values of the map and add them up one by one. Alternatively, you can use LINQ to simplify the process.

Here is an example of iterating through the values of a map and summing them up:

main.cs
Dictionary<string, int> map = new Dictionary<string, int>(); // example map

int sum = 0;
foreach (int value in map.Values)
{
    sum += value;
}

Console.WriteLine("Sum of all values in map: " + sum);
202 chars
10 lines

And here is an example of using LINQ to achieve the same result:

main.cs
Dictionary<string, int> map = new Dictionary<string, int>(); // example map

int sum = map.Values.Sum();

Console.WriteLine("Sum of all values in map: " + sum);
161 chars
6 lines

related categories

gistlibby LogSnag