How can I detect if this dictionary key exists in C#?

后端 未结 5 424
青春惊慌失措
青春惊慌失措 2020-11-30 17:51

I am working with the Exchange Web Services Managed API, with contact data. I have the following code, which is functional, but not ideal:

foreach (         


        
相关标签:
5条回答
  • 2020-11-30 18:17

    Here is a little something I cooked up today. Seems to work for me. Basically you override the Add method in your base namespace to do a check and then call the base's Add method in order to actually add it. Hope this works for you

    using System;
    using System.Collections.Generic;
    using System.Collections;
    
    namespace Main
    {
        internal partial class Dictionary<TKey, TValue> : System.Collections.Generic.Dictionary<TKey, TValue>
        {
            internal new virtual void Add(TKey key, TValue value)
            {   
                if (!base.ContainsKey(key))
                {
                    base.Add(key, value);
                }
            }
        }
    
        internal partial class List<T> : System.Collections.Generic.List<T>
        {
            internal new virtual void Add(T item)
            {
                if (!base.Contains(item))
                {
                    base.Add(item);
                }
            }
        }
    
        public class Program
        {
            public static void Main()
            {
                Dictionary<int, string> dic = new Dictionary<int, string>();
                dic.Add(1,"b");
                dic.Add(1,"a");
                dic.Add(2,"c");
                dic.Add(1, "b");
                dic.Add(1, "a");
                dic.Add(2, "c");
    
                string val = "";
                dic.TryGetValue(1, out val);
    
                Console.WriteLine(val);
                Console.WriteLine(dic.Count.ToString());
    
    
                List<string> lst = new List<string>();
                lst.Add("b");
                lst.Add("a");
                lst.Add("c");
                lst.Add("b");
                lst.Add("a");
                lst.Add("c");
    
                Console.WriteLine(lst[2]);
                Console.WriteLine(lst.Count.ToString());
            }
        }
    }
    
    0 讨论(0)
  • 2020-11-30 18:22

    I use a Dictionary and because of the repetetiveness and possible missing keys, I quickly patched together a small method:

     private static string GetKey(IReadOnlyDictionary<string, string> dictValues, string keyValue)
     {
         return dictValues.ContainsKey(keyValue) ? dictValues[keyValue] : "";
     }
    

    Calling it:

    var entry = GetKey(dictList,"KeyValue1");
    

    Gets the job done.

    0 讨论(0)
  • 2020-11-30 18:28

    You can use ContainsKey:

    if (dict.ContainsKey(key)) { ... }
    

    or TryGetValue:

    dict.TryGetValue(key, out value);
    

    Update: according to a comment the actual class here is not an IDictionary but a PhysicalAddressDictionary, so the methods are Contains and TryGetValue but they work in the same way.

    Example usage:

    PhysicalAddressEntry entry;
    PhysicalAddressKey key = c.PhysicalAddresses[PhysicalAddressKey.Home].Street;
    if (c.PhysicalAddresses.TryGetValue(key, out entry))
    {
        row["HomeStreet"] = entry;
    }
    

    Update 2: here is the working code (compiled by question asker)

    PhysicalAddressEntry entry;
    PhysicalAddressKey key = PhysicalAddressKey.Home;
    if (c.PhysicalAddresses.TryGetValue(key, out entry))
    {
        if (entry.Street != null)
        {
            row["HomeStreet"] = entry.Street.ToString();
        }
    }
    

    ...with the inner conditional repeated as necessary for each key required. The TryGetValue is only done once per PhysicalAddressKey (Home, Work, etc).

    0 讨论(0)
  • 2020-11-30 18:32

    PhysicalAddressDictionary.TryGetValue

     public bool TryGetValue (
        PhysicalAddressKey key,
        out PhysicalAddressEntry physicalAddress
         )
    
    0 讨论(0)
  • 2020-11-30 18:35

    What is the type of c.PhysicalAddresses? If it's Dictionary<TKey,TValue>, then you can use the ContainsKey method.

    0 讨论(0)
提交回复
热议问题