Dictionary <K,V> question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • =?Utf-8?B?YW1pcg==?=

    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.
  • JDeats

    #2
    Re: Dictionary &lt;K,V&gt; question

    You don't need to know the datatype for the key to iterate through the
    values.

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

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

    Comment

    • Jon Skeet [C# MVP]

      #3
      Re: Dictionary &lt;K,V&gt; question

      amir <amir@discussio ns.microsoft.co mwrote:
      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 - <skeet@pobox.co m>
      Web site: http://www.pobox.com/~skeet
      Blog: http://www.msmvps.com/jon.skeet
      C# in Depth: http://csharpindepth.com

      Comment

      • =?Utf-8?B?YW1pcg==?=

        #4
        Re: Dictionary &lt;K,V&gt; question

        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<stri ng, stringmyHash = new Dictionary<stri ng,
        string>();
        myHash.Add("k1" , "bob");
        myHash.Add("k2" , "jim");
        >
        foreach (string val in myHash.Values)
        {
        string firstValue = val;
        break;
        }
        >

        Comment

        • =?UTF-8?B?R8O2cmFuIEFuZGVyc3Nvbg==?=

          #5
          Re: Dictionary &lt;K,V&gt; question

          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.F irst();

          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<stri ng, stringmyHash = new Dictionary<stri ng,
          >string>();
          > myHash.Add("k1" , "bob");
          > myHash.Add("k2" , "jim");
          >>
          > foreach (string val in myHash.Values)
          > {
          > string firstValue = val;
          > break;
          > }
          >>

          --
          Göran Andersson
          _____
          Göran Anderssons privata hemsida.

          Comment

          Working...