【问题标题】:Is it possible to have dictionary where keys are of type bool?是否可以有键为 bool 类型的字典?
【发布时间】:2017-05-06 21:25:17
【问题描述】:

类似这样的:

string defectGuid = null;

var dictionary = new Dictionary<bool,string> {
    {new ProjectDiscrepancyWrongLocation().Conditions(row), "88ff2dfb-6190-4ab6-b13b-68de1719eac2"},
    {new DestructionLeakiness().Conditions(row), "af018ee7-7974-45f8-a508-18359cde4108"},
    {new CoatingInsulationDefect().Conditions(row), "232b2b2e-abc0-46b2-8b8c-45fede83ad83"},
    {new CathodicProtectionUnitUnderProtectionZone().Conditions(row), "78cb3548-0d61-4809-bf28-0590dfb52010"},
    {new CoatingPaintDefect().Conditions(row), "83e82b20-efc9-4500-8ca5-bb29631ccc61"},
    {new ProjectDiscrepancyEdgesOffset().Conditions(row), "cf9c76c3-79eb-4856-8dc6-c9c7321a3ed5"},
    {new DocumentationDiscrepancyDiscrepanciesInDocumentation().Conditions(row), "d3c793bc-5840-413a-8cae-6962772b4a88"},
    {new DestructionElementDestruction().Conditions(row), "6b8924f9-5bba-4db8-b33c-099ca17a58b9"},
    {new IndicatorDefectDamage().Conditions(row), "8F2560F3-31A0-4489-9A77-387BE9347D38"}
};
foreach (var item in dictionary.Where(item => item.Key)) {
    defectGuid = item.Value;
}
return defectGuid;

其中一个类如下:

public class DestructionLeakiness : IDefectTypeConditions {
    public bool Conditions(string[] row) {
        return Helpers.NormalizeLocalizedString(row[4]).Contains("aaaa");
    }
}

问题是会有一个错误提示密钥已经存在。

更新:

我改变了以下方法:

private static string SetTypeOfDefect(string[] row)
{
    string defectGuid = null;

    var list = new List<Tuple<bool, string>>()
    {
        new Tuple<bool, string>(new ProjectDiscrepancyWrongLocation().Conditions(row), "88ff2dfb-6190-4ab6-b13b-68de1719eac2"),
        new Tuple<bool, string>(new DestructionLeakiness().Conditions(row), "af018ee7-7974-45f8-a508-18359cde4108"),
        new Tuple<bool, string>(new CoatingInsulationDefect().Conditions(row), "232b2b2e-abc0-46b2-8b8c-45fede83ad83"),
        new Tuple<bool, string>(new CathodicProtectionUnitUnderProtectionZone().Conditions(row), "78cb3548-0d61-4809-bf28-0590dfb52010"),
        new Tuple<bool, string>(new CoatingPaintDefect().Conditions(row), "83e82b20-efc9-4500-8ca5-bb29631ccc61"),
        new Tuple<bool, string>(new ProjectDiscrepancyEdgesOffset().Conditions(row), "cf9c76c3-79eb-4856-8dc6-c9c7321a3ed5"),
        new Tuple<bool, string>(new DocumentationDiscrepancyDiscrepanciesInDocumentation().Conditions(row), "d3c793bc-5840-413a-8cae-6962772b4a88"),
        new Tuple<bool, string>(new DestructionElementDestruction().Conditions(row), "6b8924f9-5bba-4db8-b33c-099ca17a58b9"),
        new Tuple<bool, string>(new IndicatorDefectDamage().Conditions(row), "8F2560F3-31A0-4489-9A77-387BE9347D38")
    };

    foreach (var item in list.Where(item => item.Item1))
    {
        defectGuid = item.Item2;
    }

    return defectGuid;
}

【问题讨论】:

  • 当然,为什么不可能呢?你试过了吗?发生了什么?
  • 是的,有两个键
  • 当然可以,但字典中只能有两个实例
  • 你认为字典是如何工作的,你想通过使用布尔值作为键类型的字典来解决什么问题?
  • 字典键必须是唯一的。你是如何尝试使用它的?也许你误解了字典的用途......

标签: c# dictionary collections


【解决方案1】:

您不能使用布尔字典,因为它仅限于两个值。这将不可避免地导致密钥冲突,因为您计划向其中添加两个以上的项目。好消息是无论如何您都不需要字典,因为您将它用作元组的集合。这将起作用:

var tupleList = new List<Tuple<bool,string>> {
    Tuple.Create(new ProjectDiscrepancyWrongLocation().Conditions(row), "88ff2dfb-6190-4ab6-b13b-68de1719eac2")
,   Tuple.Create(new DestructionLeakiness().Conditions(row), "af018ee7-7974-45f8-a508-18359cde4108")
,   Tuple.Create(new CoatingInsulationDefect().Conditions(row), "232b2b2e-abc0-46b2-8b8c-45fede83ad83"), ...
};
return tupleList.FirstOrDefault(t => t.Item1)?.Item2;

就带有布尔键的字典而言,当您需要 Dictionary 键时,bool 与任何其他带有哈希码和等号的类型一样好。

但是,布尔值字典不是一个非常有效的数据结构,因为您最多可以存储两个字符串 - 每个值一个对应 bool。您可以使用包含两个字符串的数组完成(几乎*)相同的事情,并使用cond ? 1 : 0 表达式对它们进行索引:

string messages[] = new string[2] { "Message on false", "Message on true"};
var msg = messages[condition ? 1 : 0];

* 字典可让您区分未设置键与将键设置为 null 值的情况,而数组则不允许您这样做。

【讨论】:

  • 在不知道您的更新而不是字典的情况下,我使用了包含布尔和字符串的元组列表。
猜你喜欢
  • 1970-01-01
  • 2019-10-05
  • 2013-12-03
  • 1970-01-01
  • 2014-04-14
  • 2013-10-14
  • 1970-01-01
  • 2021-10-02
  • 2021-06-14
相关资源
最近更新 更多