通过获取密钥词典值 [英] get dictionary value by key

查看:242
本文介绍了通过获取密钥词典值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何可以通过功能键得到字典值

How i can get the dictionary value by key on function

我的功能代码是这样的(和命令我做的尝试,但没有工作):

my function code is this ( and the command what i try but didnt work ):

static void XML_Array(Dictionary<string, string> Data_Array)
{
    String xmlfile = Data_Array.TryGetValue("XML_File", out value);
}



我的按钮的代码是这样

my button code is this

private void button2_Click(object sender, EventArgs e)
{
    Dictionary<string, string> Data_Array = new Dictionary<string, string>();
    Data_Array.Add("XML_File", "Settings.xml");

    XML_Array(Data_Array);
}



我想是这样的:

上XML_Array功能被

串XMLFILE =将Settings.xml

I want something like this:
on XML_Array function to be
string xmlfile = Settings.xml

推荐答案

这是如此简单:

String xmlfile = Data_Array["XML_File"];

请注意,如果字典中没有等于XML_FILE的关键,该代码会抛出异常。如果你想先检查一下,你可以使用TryGetValue这样的:

Note that if the dictionary doesn't have a key that equals "XML_File", that code will throw an exception. If you want to check first, you can use TryGetValue like this:

string xmlfile;
if (!Data_Array.TryGetValue("XML_File", out xmlfile)) {
   // the key isn't in the dictionary.
   return; // or whatever you want to do
}
// xmlfile is now equal to the value

这篇关于通过获取密钥词典值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆