473,322 Members | 1,494 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

Dictionary <K,V> question

how can I access the first item of a dictionary where the datatype for the
Key is not known to me?

I tried the array index 0 and taht did not work because the key is not of
integer type.

Thanks in advance for all your help.
Aug 6 '08 #1
4 1468
You don't need to know the datatype for the key to iterate through the
values.

Dictionary<string, stringmyHash = new Dictionary<string,
string>();
myHash.Add("k1", "bob");
myHash.Add("k2", "jim");

foreach (string val in myHash.Values)
{
string firstValue = val;
break;
}
Aug 6 '08 #2
amir <am**@discussions.microsoft.comwrote:
how can I access the first item of a dictionary where the datatype for the
Key is not known to me?
What exactly do you mean by "first"? Dictionaries are inherently
unordered. You could use the enumerator to find the first value to come
out, but there's no guarantee that was the first which was put in.
I tried the array index 0 and taht did not work because the key is not of
integer type.
If you don't know the generic type parameters of the dictionary, how
are you accessing it? You may need to use reflection to get at the bits
of the item if you don't know the type parameters.

--
Jon Skeet - <sk***@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com
Aug 6 '08 #3
I knew of this one already but I was looking to see if a smarter and quicker
way existed.

Thanks much for your quick response

"JDeats" wrote:
You don't need to know the datatype for the key to iterate through the
values.

Dictionary<string, stringmyHash = new Dictionary<string,
string>();
myHash.Add("k1", "bob");
myHash.Add("k2", "jim");

foreach (string val in myHash.Values)
{
string firstValue = val;
break;
}
Aug 6 '08 #4
amir wrote:
I knew of this one already but I was looking to see if a smarter and quicker
way existed.

Thanks much for your quick response
There is no better way, as you can't access the items by index.

In framework 3.5 you can use one of the extension methods:

string firstValue = muHash.Values.First();

It does it the same way, though.
"JDeats" wrote:
>You don't need to know the datatype for the key to iterate through the
values.

Dictionary<string, stringmyHash = new Dictionary<string,
string>();
myHash.Add("k1", "bob");
myHash.Add("k2", "jim");

foreach (string val in myHash.Values)
{
string firstValue = val;
break;
}

--
Göran Andersson
_____
http://www.guffa.com
Aug 6 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Christian Schmidbauer | last post by:
Hello! I prepare my XML document like this way: ------------------------------------------------------- PrintWriter writer; Document domDocument; Element domElement; // Root tag
4
by: higabe | last post by:
Three questions 1) I have a string function that works perfectly but according to W3C.org web site is syntactically flawed because it contains the characters </ in sequence. So how am I...
2
by: ESPNSTI | last post by:
Hi, I'm trying to use a generics dictionary with a key class that implements and needs IComparable<>. However when I attempt to use the dictionary, it doesn't appear to use the IComparable<> to...
8
by: Brian P | last post by:
I want to expose a property of Dictionary<string, MyAbstractClass>. I tried to do it this way: private Dictionary<string, MyChildClass> _dictionary; public Dictionary<string,...
6
by: buzzweetman | last post by:
Many times I have a Dictionary<string, SomeTypeand need to get the list of keys out of it as a List<string>, to pass to a another method that expects a List<string>. I often do the following: ...
7
by: Andrew Robinson | last post by:
I have a method that needs to return either a Dictionary<k,vor a List<v> depending on input parameters and options to the method. 1. Is there any way to convert from a dictionary to a list...
2
by: Assimalyst | last post by:
Hi I have a Dictionary<string, List<string>>, which i have successfully filled. My problem is I need to create a filter expression using all possible permutations of its contents. i.e. the...
6
by: Paul.N.Phillips | last post by:
I am using a static dictionary to objects (like cache) but woundered if it is better to use cache. Which one would should I use?
2
by: Andy B | last post by:
I don't know if this is even working or not but here is the problem. I have a gridview that I databound to a dictionary<string, stringcollection: Contract StockContract = new Contract();...
9
by: eric | last post by:
I'm trying to define a dictionary whose value is an Generic Action<> delegate private Dictionary<string, List<Action<T>>> Any ideas on to how specify T or a different collection type more...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.