gpt4 book ai didi

c# - 初始化字典 "on the fly"

转载 作者:太空宇宙 更新时间:2023-11-03 23:20:00 24 4
gpt4 key购买 nike

我有以下类(class)

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
});
}

最佳答案

它需要像...

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

关于c# - 初始化字典 <T,T> "on the fly",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35745212/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com