是否可以使用字典键,其中键是bool类型?

时间:2016-12-21 12:39:42

标签: c# dictionary collections

这样的事情:

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

1 个答案:

答案 0 :(得分:5)

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

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值的情况,而数组则不允许您这样做。