c# - C# 中缺少 Dictionary(of String, String) Item 属性

标签 c# vb.net dictionary

我正在将此 VB.NET 代码转换为 C#。

Dim dict As New Dictionary(Of String, String)

If dict.TryGetValue(key, val) Then
   dict.Item(key) = val & "~" & row.Item(y).ToString
Else
   dict.Add(key, row.Item(y).ToString)
End If

这是 C# 代码。注意 If dict.TryGetValue 之后的行。 Intellisense 不显示 C# 中的 Item 属性。 C# 的正确语法是什么?

Dictionary<string, string> dict = new Dictionary<string, string>();

if (dict.TryGetValue(key, out val)) {
    dict.Item[key] = val + "~" + row.ItemArray[y].ToString();
} else {
    dict.Add(key, row.ItemArray[y].ToString());
}

最佳答案

在 C# 中访问字典中键的值的方式如下:

dict[key]

话虽这么说:

dict.Item[key] = val + "~" + row.ItemArray[y].ToString();

应该改变如下:

dict[key] = val + "~" + row.ItemArray[y].ToString();

关于c# - C# 中缺少 Dictionary(of String, String) Item 属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42653533/

相关文章:

C# 十进制,如何添加尾随零

c# - 在 WPF 中更改 Combobox 的边框颜色

c# - WPF: Canvas Z 索引错误

c# - 不同值类型之间的除以零行为不一致

python - 编写标准差函数

c# - 从 Oracle 读取问题

vb.net - 如何正确读取单个 Excel 单元格

java - 映射 Avro 模型

python - Sqlalchemy 对 dict 列表的查询

vb.net - 如何防止鼠标滚轮在不过载的情况下增加 numericupdown ?