get dictionary value by key

前端 未结 10 1826
逝去的感伤
逝去的感伤 2020-11-29 20:47

How can I get the dictionary value by key on function

my function code is this ( and the command what I try but didn\'t work ):

static void XML_Array         


        
相关标签:
10条回答
  • 2020-11-29 21:22

    Here is an example which I use in my source code. I am getting key and value from Dictionary from element 0 to number of elements in my Dictionary. Then I fill my string[] array which I send as a parameter after in my function which accept only params string[]

    Dictionary<string, decimal> listKomPop = addElements();
    int xpopCount = listKomPop.Count;
    if (xpopCount > 0)
    {
        string[] xpostoci = new string[xpopCount];
        for (int i = 0; i < xpopCount; i++)
        {
            /* here you have key and value element */
            string key = listKomPop.Keys.ElementAt(i);
            decimal value = listKomPop[key];
    
            xpostoci[i] = value.ToString();
        }
    ...
    

    This solution works with SortedDictionary also.

    0 讨论(0)
  • 2020-11-29 21:23
    if (Data_Array["XML_File"] != "") String xmlfile = Data_Array["XML_File"];
    
    0 讨论(0)
  • 2020-11-29 21:24

    I use a similar method to dasblinkenlight's in a function to return a single key value from a Cookie containing a JSON array loaded into a Dictionary as follows:

        /// <summary>
        /// Gets a single key Value from a Json filled cookie with 'cookiename','key' 
        /// </summary>
        public static string GetSpecialCookieKeyVal(string _CookieName, string _key)
        {
            //CALL COOKIE VALUES INTO DICTIONARY
            Dictionary<string, string> dictCookie =
            JsonConvert.DeserializeObject<Dictionary<string, string>>
             (MyCookinator.Get(_CookieName));
    
            string value;
            if (dictCookie.TryGetValue( _key, out value))
            {
                return value;
            }
            else
            {
                return "0";
            }
    
        }
    

    Where "MyCookinator.Get()" is another simple Cookie function getting an http cookie overall value.

    0 讨论(0)
  • 2020-11-29 21:30
    Dictionary<String,String> d = new Dictionary<String,String>();
            d.Add("1","Mahadev");
            d.Add("2","Mahesh");
            Console.WriteLine(d["1"]);// it will print Value of key '1'
    
    0 讨论(0)
提交回复
热议问题