Question
Do I need to scroll through all the elements of the C # Dictionary only through foreach , and if so, why?
Or, I could ask my question how: is it possible to get Dictionary elements by position inside the Dictionary object (i.e. the first element, the last element, the third from the last element, etc.)?
Background information on this subject.
I am trying to learn more about how Dictionary objects work, so I will be grateful for the help in this. I find out about this, so I have a few thoughts that are all related to this issue. I will try to present in a way that is suitable for the SO format.
Study
In a C # array, elements refer to a position. In Dictionary values refer to keys.
Looking through the documentation on MSDN , there are instructions
"For enumeration purposes, each element in the dictionary is treated as a KeyValuePair structure representing the value and its key. The order in which the elements are returned is undefined."
So, it would seem that since order items are returned in undefined format, there is no way to access items by position. I also read:
"Getting the value with its key is very fast, close to O (1), because the Dictionary class is implemented as a hash table."
Looking at the documentation for the HashTable .NET 4.5 class , there is a link to using the foreach for looping and return elements. But there is no reference to using the for statement, or, for that matter, while or any other loop statement.
In addition, I noticed that Dictionary elements use the IEnumerable interface, which apparently uses foreach as the only type of statement for loop functions.
Thoughts
So, does this mean that Dictionary elements cannot be accessed by "position", as arrays or lists can?
If so, why is there a .Count property that returns the number of key / value pairs, but nothing that allows me to refer to their proximity to the total? For example .Count is 5, why can't I request a key / value .Count minus 1?
How foreach can iterate over each element, but I do not have access to individual elements in the same way?
Is there no way to determine the position of an element (key or value) in a Dictionary object without using foreach ? May I not say without binding the elements to the collection if the key is the first key in the Dictionary or the last key?
This SO question and excellent answers relate to this, but I specifically look to see if I need to copy elements to an array or other enumerated type to allow access to certain elements by position.
Here is an example. Please note that I am not looking for a way to specifically solve this example - this is only to illustrate my questions. Suppose I want to add all the keys in a Dictionary<string, string> object to a comma-separated list, with no comma at the end. With an array I could do:
string[] arrayStr = new string[2] { "Test1", "Test2" }; string outStr = ""; for (int i = 0; i < arrayStr.Length; i++) { outStr += arrayStr[i]; if (i < arrayStr.Length - 1) { outStr += ", "; } }
With Dictionary<string, string> , how can I copy each key to outStr using the above method? It looks like I would have to use foreach . But what are the methods or properties of the Dictionary that would allow me to determine where the element is located inside the dictionary?
If you are still reading this, I also want to indicate that I am not trying to say that something is wrong with the Dictionary ... I am just trying to understand how this tool works in the .NET platform, and how best to use it myself .