[Solved] Is it possible to have dictionary where keys are of type bool?


You cannot use a dictionary of Booleans, because it is limited to only two values. This will inevitably lead to key collisions, because you plan to add more than two items to it. Good news is that you do not need a dictionary anyway, because you use it as a collection of tuples. This will work:

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;

As far as a dictionary with Boolean keys is concerned, when you need a Dictionary key, bool is as good as any other type with hash code and equals.

A dictionary on a Boolean would not be a very efficient data structure, though, because you would be able to store a maximum of two strings – one for each value of bool. You can accomplish (nearly*) the same thing with an array of two strings, and indexing them with cond ? 1 : 0 expression:

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

* Dictionary would let you distinguish between a situation when a key is not set vs. a key is set to null value, while an array wouldn’t let you do it.

0

solved Is it possible to have dictionary where keys are of type bool?