如何通过函数上的键获取字典值?
我的函数代码(和我尝试的命令不工作):
static void XML_Array(Dictionary<string, string> Data_Array)
{
String xmlfile = Data_Array.TryGetValue("XML_File", out value);
}
我的按钮代码:
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函数的变量是:
string xmlfile = "Settings.xml":
下面是我在源代码中使用的一个例子。
我从字典中获取键和值,从元素0到我的字典中的元素数。然后填充string[]数组将其作为参数发送到只接受参数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();
}
...
这个解决方案也适用于SortedDictionary。
下面是我在源代码中使用的一个例子。
我从字典中获取键和值,从元素0到我的字典中的元素数。然后填充string[]数组将其作为参数发送到只接受参数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();
}
...
这个解决方案也适用于SortedDictionary。