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 / Languages / C# / July 2007

Tip: Looking for answers? Try searching our database.

Generic Dictionaries and Indexes

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Ethan Strauss - 26 Jul 2007 22:41 GMT
Hi,
  I have been using a variety of generic collections and I really like
them, but I have just noticed something weird ...

If you have a generic Dictionary (or SortedList), you can retrieve elements
from the list by index (I think).
So:
   Dictionary<string, string> ThisStringDictionary = new Dictionary<string,
string>();
   ---Code to populate dictionary---
   string ThisValue = ThisStringDictionary[0]; //This should get you the
value of the first element of the dictionary and throw no errors. Right?

I have a SortedList which is defined as
   SortedList <string, MyCustomType> MySortedList = new  SortedList
<string, MyCustomType>();
but, the fact is that almost all of the keys are, in fact integers being
treated as strings.

So I have situations where I have something like :
    SortedList <string, MyCustomType> MySortedList = new  SortedList
<string, MyCustomType>();
    ---Code to populate MySortedList ---
but now
   MySortedList [0] != MySortedList ["0"];

This is all OK, the computer differentiates the two just fine.

My real question is what if I have a SortedList <int, anyType>? How can it
tell if I am asking for an element by index or by key?
I am guessing that the fact that I have this questions means I am missing
something. Can anyone fill me in?
Thanks!
Ethan

Ethan Strauss Ph.D.
Bioinformatics Scientist
Promega Corporation
2800 Woods Hollow Rd.
Madison, WI 53711
608-274-4330
800-356-9526
ethan.strauss@promega.com
sdbillsfan@gmail.com - 26 Jul 2007 22:57 GMT
> So:
>     Dictionary<string, string> ThisStringDictionary = new Dictionary<string,
> string>();
>     ---Code to populate dictionary---
>     string ThisValue = ThisStringDictionary[0]; //This should get you the
> value of the first element of the dictionary and throw no errors. Right?

If you had taken 10 seconds to verify this using the documentation you
could have saved yourself the trouble of writing this post and us the
trouble of reading it :-)
Jon Skeet [C# MVP] - 26 Jul 2007 23:08 GMT
>    I have been using a variety of generic collections and I really like
> them, but I have just noticed something weird ...
>
> If you have a generic Dictionary (or SortedList), you can retrieve elements
> from the list by index (I think).

Dictionary? No. There *is* no index as such.

SortedList? Yes - but only via the Values property (which implements
IList<TKey>).

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.