Iterating through a dictionary without foreach  
Author Message
Carl Bateman





PostPosted: .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





PostPosted: .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





PostPosted: .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





PostPosted: .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





PostPosted: .NET Base Class Library, Iterating through a dictionary without foreach Top

It would be helpful if you gave more information about the data and why you placed it in the dictionary in the first place.
I inherited the code hence the Dictionary.

But to give you a straight answer, no, you cannot directly access dictionary elements by position, unless... <snip>
I thought not.

The code snippet illustrates a specific algorithm I'm trying to implement, I wish to compare each item with every other item in the dictionary once and once only, while avoiding self comparison. The actual "order" of the items is entirely irrelevant, I'm just trying to avoid redundant tests.

In the end I've switched to an ArrayList which seems to not break existing code while allowing me to implement the algorithm.

Many thanks to everyone for the feedback.


 
 
Matthew Watson





PostPosted: .NET Base Class Library, Iterating through a dictionary without foreach Top

Just so you know, there's a semi-official class library that provides a lot of the "missing" collection classes, such as OrderedDictionary<TKey,TValue>

It's called PowerCollections, and you can get it at http://www.wintellect.com/PowerCollections.aspx

 
 
Santiago Perez





PostPosted: .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