初始化字典“即时”

本文关键字:即时 字典 初始化 | 更新日期: 2023-09-27 18:36:59

我有以下类

public class PLCWrapper
{
    public int plc_id { get; set; }
    public List<SchakelingWrapper> schakelingWrapperList { get; set; }
    public Dictionary<string, string> globalVarDict { get; set; }
}
public class SchakelingWrapper
{
    public int schakeling_id { get; set; }
    public Dictionary<string, string> varDict { get; set; }
}

我想做下面这样的事情,但无法修复。确定有人知道怎么做吗?

if (plcWrapper.schakelingWrapperList.Exists(item => item.schakeling_id == id)) {
    plcWrapper.schakelingWrapperList.Single(item => item.schakeling_id == id).varDict.Add(varName, varValue);
} else {
    plcWrapper.schakelingWrapperList.Add(new SchakelingWrapper()
    {
        schakeling_id = id,
        varDict = new Dictionary<string, string>(varName, varValue)//This does not work
    });
}

初始化字典<T,T>“即时”

它需要像...

varDict = new Dictionary<string, string>()
{
    {varname, varvalue}
}

将此行从

varDict = new Dictionary<string, string>(varName, varValue)//This does not work

varDict = new Dictionary<string, string>() {{varName, varValue}}