There are two ways for C# Dictionary to determine whether there is a key Key

Define Dictionary object and add content

                Dictionary<string, string> dict = new Dictionary<string, string>() { };
                dict.Add("a", "a1");
                dict.Add("b", "b1");
                dict.Add("c", "c1");
                string sKey = "a";

Judgment method 1:

                if (dict.Keys.Any() = (e) => { 0 == string.Compare(e, sKey); })
                {
                    
                }

Judgment method 2:

                string sValue = "";
                if (dict.TryGetValue(sKey, out sValue))
                {

                }

Method 2 is recommended.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325547260&siteId=291194637