C# 在C中更改嵌入式词典的值#

C# 在C中更改嵌入式词典的值#,c#,dictionary,unity3d,C#,Dictionary,Unity3d,我有一本这样的字典: public Dictionary<string, Dictionary<List<string>, bool>> dictionary= new Dictionary<string, Dictionary<List<string>, bool>>(); 您的词典定义可能是正确的(不太可能,但如果没有更好的问题澄清,很难确定) 公共字典=新字典(); 不过,在scond字典中有一个键看起来不太可能。

我有一本这样的字典:

public Dictionary<string, Dictionary<List<string>, bool>> dictionary= new Dictionary<string, Dictionary<List<string>, bool>>();

您的词典定义可能是正确的(不太可能,但如果没有更好的问题澄清,很难确定)

公共字典=新字典();
不过,在scond字典中有一个键看起来不太可能。 我怀疑这就是你的意思

public Dictionary<string, List<(string, bool)> dictionary= new Dictionary<string,List<(string, bool)>();

公共字典您可以使用字典,但不能使用
列表
!为了从
字典
访问相应的元素,您总是需要完全相同的列表引用

相反,将内部的一个只作为一个
字典
,用于将一个键映射到一个值


出于可读性和可维护性的原因,我宁愿创建合适的包装器类,比如

public class GridCell 
{ 
    public Dictionary<string, bool> Properties = new Dictionary<string, bool>(); 
}
 
public void SetProperties(string indexName, List<string> properties, bool value)
{
    // creates the new field if it didn't exist so far
    if(!Grid.ContainsKey(indexName)) Grid[indexName] = new Dictionary<string, bool>();

    foreach(var property in properties)
    {
        // This either updates an existing property entry
        // or creates a new one if it didn't exist so far
        Grid[indexName][property] = value;
    }
}
同时设置多个属性,而不是实现类似

public class GridCell 
{ 
    public Dictionary<string, bool> Properties = new Dictionary<string, bool>(); 

    public void SetProperties(List<string> properties, bool value)
    {
        foreach(var property in properties)
        {
            // Either updates the entry if it exists already
            // or adds a new entry for this key
            Properties[property] = value;
        }
    }
}
然后在相应地初始化它之后,您可以访问一个特定的值,如

var hasProperty = Grid[indexName][propertyName];

Grid[indexName][propertyName] = true;
为了一次设置多个值,您应该实现自己的方法,例如

public class GridCell 
{ 
    public Dictionary<string, bool> Properties = new Dictionary<string, bool>(); 
}
 
public void SetProperties(string indexName, List<string> properties, bool value)
{
    // creates the new field if it didn't exist so far
    if(!Grid.ContainsKey(indexName)) Grid[indexName] = new Dictionary<string, bool>();

    foreach(var property in properties)
    {
        // This either updates an existing property entry
        // or creates a new one if it didn't exist so far
        Grid[indexName][property] = value;
    }
}
public void SetProperties(字符串索引名、列表属性、bool值)
{
//如果新字段目前不存在,则创建该字段
如果(!Grid.ContainsKey(indexName))Grid[indexName]=new Dictionary();
foreach(属性中的var属性)
{
//这会更新现有的属性条目
//或者创建一个新的,如果它还不存在的话
网格[indexName][property]=值;
}
}

我更喜欢顶部的解决方案,以获得更好的可维护性。您可以轻松地在
GridCell
类中实现更多方法,或者更改和扩展现有方法,而不会对外部造成太大影响。您还可以轻松地将与网格相关的内容分离(解耦)到一个额外的类中,而无需使用太多的方法将
monobhavior
组件弄乱


在第二种方法中,您将以一个超级强大的类结束,该类在这个嵌套的
字典

中完成全部工作,对
字典
键使用
列表
没有多大意义,因为它将对
列表
的引用进行哈希运算,而不是对列表中的值进行哈希运算,除非您创建一个
IEqualityComparer
来为您执行此操作,并在创建字典时传入tn。如果您需要一个正好包含两个字符串值的键,则可以改为执行
dictionary
dictionary
。这是一个。这本词典似乎非常错误。它应该做什么并像这样使用?
List
s是引用对象,这意味着等式将只对同一个对象实例有效,因此它不会像您期望的那样工作。对于网格,不是像
public class GridCell{public Dictionary properties=new Dictionary();}
然后是
public GridCell[,]网格=新网格单元[宽度、高度]更好吗?
字典
就更没意义了,因为字典只有键和值的泛型类型,并没有第三种泛型类型。我还在编辑,你们很快就发表了评论;)在正确格式化答案之前不要发布帖子,否则会发生类似的事情;)(我删除了我的否决票,现在这是有意义的)这正是网络的本质,如果你充分阐述了你的答案并把它们弄对了(就像我过去所做的那样),那么在你大部分时间完成写作之前,接受的答案会在10分钟后发布。我只是想给出一个答案来获得我需要的几点意见,这样我就可以帮助人们在激烈的声誉竞争中获得分数
public class GridCell 
{ 
    public Dictionary<string, bool> Properties = new Dictionary<string, bool>(); 

    public void SetProperties(List<string> properties, bool value)
    {
        foreach(var property in properties)
        {
            // Either updates the entry if it exists already
            // or adds a new entry for this key
            Properties[property] = value;
        }
    }
}
public void SetProperties(int gridX, int gridY, List<string> properties, bool value)
{
    // creates the new field if it didn't exist so far
    if(Grid[gridX, gridX] == null)) Grid[gridX, gridY] = new GridCell();

    Grid[gridX, gridY].SetProperties(properties, value);
}
public Dictionary<string, Dictionary<string, bool>> Grid = new Dictionary<string, Dictionary<string, bool>>();
var hasProperty = Grid[indexName][propertyName];

Grid[indexName][propertyName] = true;
public void SetProperties(string indexName, List<string> properties, bool value)
{
    // creates the new field if it didn't exist so far
    if(!Grid.ContainsKey(indexName)) Grid[indexName] = new Dictionary<string, bool>();

    foreach(var property in properties)
    {
        // This either updates an existing property entry
        // or creates a new one if it didn't exist so far
        Grid[indexName][property] = value;
    }
}