M
M
Maxim Isaev2018-10-27 23:20:08
.NET
Maxim Isaev, 2018-10-27 23:20:08

How to refer to a key element in a dictionary?

Have a dictionary

var wordCount = new Dictionary<KeyValuePair<string, string>, int>();

I want to access the first element of the key, for example, if the key is [x, y], get x. How to implement? Thank you

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Sergey V, 2018-10-28
@dthpth

wordCount.First().Key.Key

S
Stanislav Makarov, 2018-10-28
@Nipheris

1. If we are talking ONLY about the key, why do you need to know what dictionary you have? Either you gave too much information, or you didn’t ask what you really wanted.
2. The question boils down to how to get the "first" element from the KeyValuePair. This can be done using the Key property. In general, read about tuples (which are built-in, in parentheses), and do it humanly: Access to the first element in this case is the Item1 property.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question