Add all keys to dictionary <int, string> in C #

This may be a very stupid / stupid question, but my defense is that I'm new!

Suppose I have a dictionary in C #:

Dictionary<int,string> D = new Dictionary<int,string>();

If I wanted to add all the values ​​(which are strings) instead of looping and adding all the values ​​to stringBuilder, I do:

string.Join(",",D.values.ToArray());

which works great. Now, if I want to add all the keys (which are int) to the total, is there a similar way to do this? I don’t want to scroll (if that's the only way) every element and add them. I'm not talking about D.Add (), which adds a new element, but adds Math, such as Key 1 + key 2, etc.

Thank!

+3
source share
3 answers
D.Keys.Sum();

, , ,

+4

, , , " ".

var total = D.Keys.Sum()
+2
int x = D.Keys.Sum(); or D.Keys.ToList<int>().Sum()

+1

Source: https://habr.com/ru/post/1761702/


All Articles