Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / New Users / September 2006

Tip: Looking for answers? Try searching our database.

iterator in Dictionary<>?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Hyun-jik Bae - 27 Sep 2006 13:03 GMT
Is there anything correspondant to the iterator for Dictionary<>? For
example, something which is similar to std::map::iterator in C++?

Reply please. Thanks in advance.

Hyun-jik Bae
Joanna Carter [TeamB] - 27 Sep 2006 13:15 GMT
| Is there anything correspondant to the iterator for Dictionary<>? For
| example, something which is similar to std::map::iterator in C++?

Sure, there are two.

{
 Dictionary<string, MyType> dict = new Dictionary<string, MyType>();

 ...

 foreach (string s in dict.Keys)
   ...

 foreach (MyType t in dict.Values)
   ...

}

Joanna

Signature

Joanna Carter [TeamB]
Consultant Software Engineer

Carl Daniel [VC++ MVP] - 27 Sep 2006 15:31 GMT
> Is there anything correspondant to the iterator for Dictionary<>? For
> example, something which is similar to std::map::iterator in C++?

All of the framework collection classes implement IEnumerable (and
IEnumerable<T> for generic collections).  IEnumerable provides a simple
iterator-like in-order enumeration of the collection.

The exact type of the enumerator for a collection is generally a nested,
private class, so you never actually declare variables of those types.
Instead, you use the IEnumerable ( <T> ) interface type.

Using C++/CLI or C# (I don't know about VB.NET) you usually don't even
declare variables of type IEnumerable.  Rather, you make use of the built-in
language feature known as foreach.

foreach (T t in collection)
{
}

is syntactic sugar for:

IEnumerable<T> it = collection.GetEnumerator();
while (it.MoveNext())
{
   T t = it.Current;
   // ...
}

In the case of System.Collections.Generic.Dictionary, there are 3 possible
iterators:

Dictionary<K,V> dict = ...

dict.GetEnumerator() - returns
IEnumerable<System.Collections.Generic.KeyValuePair<K,V>>.  This is most
closely equivalent to std::map::iterator (which iterates over
std::pair<K,V>).

dict.Keys.GetEnumerator() - returns IEnumerable<K>.  This iterator
enumerates the keys in the dictionary.

dict.Values.GetEnumerator() - returns IEnumerable<V>.  This iterator
enumerates the values in the dictionary in the same order that the keys are
returned by dict.Keys.GetEnumerator().

-cd
Jon Skeet [C# MVP] - 27 Sep 2006 21:30 GMT
> Is there anything correspondant to the iterator for Dictionary<>? For
> example, something which is similar to std::map::iterator in C++?
>
> Reply please. Thanks in advance.

There's the Keys property to return an enumeration of the keys, the
Values property to return an enumeration of the values, or just call
GetEnumerator() (or use foreach) to get an enumeration of KeyValuePair
items.

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.