c# 检查字典中是否存在键,然后传递其值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29826482/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 04:18:39  来源:igfitidea点击:

c# check if key exists in dictionary then pass on its value

c#dictionary

提问by cd300

In my desktop C#application I start with a dictionary. I want to be able to check this dictionary for a key. If the dictionary has this key, I would like to pass it on to a method. If the dictionary doesn't have this key, I would like to create a blank list and just pass that on instead. How can I do this?

在我的桌面C#应用程序中,我从一本字典开始。我希望能够检查这本字典的键。如果字典有这个键,我想把它传递给一个方法。如果字典没有这个键,我想创建一个空白列表,然后直接传递它。我怎样才能做到这一点?

I get the error "given key was not present in the dictionary". Can I add a default so it is never null maybe?

我收到错误“字典中不存在给定的密钥”。我可以添加一个默认值,使其永远不会为空吗?

// myDic was declared as a Dictionary<string, List<string>    

// Here is how I call someFunction
string text = SomeFunction(stringValue1, stringValue2, myDic[field1.field2]);

// SomeFunction looks like this
string SomeFunction (string string1, string string2, List<string> ra) 
{  
     // method 
     return stringResult;
} 

采纳答案by bkribbs

Updated based on comments. To pass one key that may or may not exist you may do this(assuming the value is a List):

根据评论更新。要传递一个可能存在也可能不存在的键,您可以这样做(假设值是一个列表):

// assuming the method we are calling is defined like this:
// public String SomeFunction(string string1, String string2, List<String> ra)  

List<string> valueToPassOn;
if (_ra.ContainsKey(lc.Lc))
{
     valueToPassOn = _ra[lc.Lc]
}
else
{
     valueToPassOn = new List<string>(); 
}

string text = tooltip.SomeFunction(something1, something2, valueToPassOn); 

Should you want to pass an entire dictionary (as the question originally read), regardless of whether or not the dictionary exists:

如果您想传递整个字典(如最初阅读的问题),无论字典是否存在:

You have two options. Either create the dictionary regardless like this:

你有两个选择。要么像这样创建字典:

if (myDic == null)
{
     // change var and var2 to the types of variable they should be, ex:
     myDic = new Dictionary<string, List<string>>();
}
string text = SomeFunction(stringValue1, stringValue2, myDic);

or, what is probably the better option, in the declaration of the function SomeFunction add a dictionary as a variable with a default parameter. Just be sure that your function knows what to do if the dictionary is null.

或者,可能是更好的选择,在函数 SomeFunction 的声明中添加一个字典作为带有默认参数的变量。只要确保您的函数知道如果字典为空该怎么办。

string SomeFunction(string string1, string string2, Dictionary dictionary = null)
{
     // method here
}

回答by Selman Gen?

You can check if the key exists using ContainsKeymethod and if it returns false you can pass a defaultvalue you want:

您可以使用ContainsKey方法检查密钥是否存在,如果返回 false,则可以传递default您想要的值:

// replace default(string) with the value you want to pass
// if the key doesn't exist
var value = myDic.ContainsKey(field1.field2) ? myDic[field1.field2] : default(string);
string text = SomeFunction(stringValue1, stringValue2, value);

回答by Pawel Rosinski

Use one of the following snippets in order to check if dictionary is empty and take some action:

使用以下代码片段之一检查字典是否为空并采取一些措施:

var x = new Dictionary<string, string>();

if (x.Any())
{
    //....
}

if (x.ContainsKey("my key"))
{

}

if (x.ContainsValue("my value"))
{

}

if (x.Count > 0)
{

}

回答by Yuval Itzchakov

What you need to do is make sure the dictionary actually contains the given key in the dictionary.

您需要做的是确保字典实际上包含字典中的给定键。

If you need to extract the value by key, use TryGetValuemethod:

如果需要按键提取值,使用TryGetValue方法:

string value;
if (myDict.TryGetValue(key, out value))
{
    // Key exists in the dictionary, do something with value.
}