字典上的返回值

时间:2016-06-03 15:45:00

标签: c# dictionary

我必须返回不添加任何值的关键部分。

关键陈述我已经写过,但我希望退出这一回报只是值。

我看过本页的ContainsKey。

http://www.dotnetperls.com/dictionary

我将“PakkeidUnik”放入您可以在此处看到的元数据字段的方式:https://stackoverflow.com/a/37605865/6115825

因此,我可以看到这里的价值:

return stripeCustomer.Metadata = new Dictionary<string, string>()
{
     Dictionary["PakkeidUnik"];//HERE ARE ITS ERROR!
};

我的错误在这里:

enter image description here

更新

enter image description here

3 个答案:

答案 0 :(得分:1)

您似乎混淆了字典启动器语法,以及基于已知密钥从字典中读取值的语法。

可以使用以下值来初始化字典:

$address = new Address((int)$id_address_invoice);

如果您想按键读取其中一个值,请使用:

var dict = new Dictionary<string,string>()
{
    {"Key1","Value1"},
    {"Key2","Value2"}
}

答案 1 :(得分:1)

有点不清楚你在问什么,但是你似乎想要获得该密钥的价值(已根据你的其他问题设定了值)。

您只需使用索引器并传递密钥:

return stripeCustomer.Metadata["PakkeidUnik"]

the article you've found中介绍了如何使用字典。您也可以在the documentation中找到有用的示例。

答案 2 :(得分:0)

请参阅以下代码:

stripeCustomer.Metadata = new Dictionary<string, string>()
{
     { "PakkeidUnik", "The Value You would like to return for key PakkeidUnik" }
};

return stripeCustomer.Metadata;