|
|
Iterating through a dictionary without foreach |
|
Author |
Message |
Carl Bateman

|
Posted: .NET Base Class Library, Iterating through a dictionary without foreach |
Top |
Working with C#.
How would I access the nth item of a Dictionary
I would like to do something like this:
for (int i = 0; i < intDictionary.Count-1; i++) { for (int j = i + 1; j < intDictionary.Count; j++) { Console.WriteLine("{0}", intDictionary.Item[j]); } }
Is this possible with a Dictionary or would I have to use a SortedList or similar
TIA
.NET Development18
|
|
|
|
 |
Dasa

|
Posted: .NET Base Class Library, Iterating through a dictionary without foreach |
Top |
SortedDictionary supports a defined ordering (sorted) of key/value pairs. Dictionary may still do the trick for you, but since the ordering of the elements is unspecified, it'd be a risky bet since you loop at least expects the ordering not to change.
|
|
|
|
 |
mmix

|
Posted: .NET Base Class Library, Iterating through a dictionary without foreach |
Top |
It would be helpfull if you gave more information about the data and why you placed it in the dictionary in the first place.
But to give you a straight answer, no, you cannot directly access dictionary elements by position, unless you used int as key type and inserted elements in consecutive order or with consecutive key (in which case Dictioray class is an overkill :)).
Dictionary class keeps all items in a private "entries" field and is kept in the order of inserting (this is unsuported though, doc states its undefined, so this could change). In this implementation, the only way to extract nth item is to retrieve enumerator and cycle through n-1 items to nth, but then you are back to foreach :). They did went through an effort to prevent you from indexing dictionary by position.
I am sure that if you explain what the data is and how and why you assemble this dictionary that a better suited approach would surface.
|
|
|
|
 |
OmegaMan

|
Posted: .NET Base Class Library, Iterating through a dictionary without foreach |
Top |
The nth item of a dictionary is a relative term, for technically the list is unsorted. The SortedList item creates a nth item...but only as specified by a sort.
If one just wants to work through a Dictionary (hash) object extract all key value pairs in a foreach and do a break on the nineth element....here is a basic example of working through the list:
| | Dictionary<string, string> ColumnValuesHash = new Dictionary<string, string>();
// ... load with values...
int index = 0; foreach (KeyValuePair<string, string> entry in ColumnValuesHash) if (index++ == 8) { Console.WriteLine("{0}: {1}", entry.Key, entry.Value); break; }
|
|
|
|
|
 |
Carl Bateman

|
Posted: .NET Base Class Library, Iterating through a dictionary without foreach |
Top |
|
|
 |
Matthew Watson

|
Posted: .NET Base Class Library, Iterating through a dictionary without foreach |
Top |
|
|
 |
Santiago Perez

|
Posted: .NET Base Class Library, Iterating through a dictionary without foreach |
Top |
I recently downloaded and was interested in using the OrderedDictionary class but can't seem to index items because it doesn't inherit OrderedList. It does Implement COllection though and I would assume I would be able to index an item but no go, can't index the values or Keys. Anyone had any luck with this Is there another library in the PowerCOllections that will allow you to find an item by either the Index or Key
Thanks,
Santiago Perez
|
|
|
|
 |
|
|