Can I make sure that in dictionary in C# there will be only a single of a specific value in it?
For example, if I define a Dictionary which key is char and value is char, can I make sure that if the character 'a' is already an existing value, there won't be another value 'a' in the dictionary?
I have a solution, but I'd like to know if there's a better one:
static void Main()
{
Dictionary<int, int> dic = new Dictionary<int, int>();
bool valExists = false;
Console.WriteLine("please enter key and value");
int key = int.Parse(Console.ReadLine());
int val = int.Parse(Console.ReadLine());
foreach (KeyValuePair<int,int> keyval in dic)
{
if (keyval.Value == val)
{
valExists = true;
break;
}
}
if (!valExists)
dic.Add(key, val);
}