Search code examples
c#.netdictionaryduplicates

How to prevent duplicate values in a Dictionary in C# .NET


I have a dictionary where values are duplicating.

How do I prevent duplicates in dictionary?

This is my code:

private class GcellGtrx
{
    public Gcell Gcell { get; set; }
    public Gtrx Gtrx { get; set; }
}

Dictionary<int, GcellGtrx> dictionary = new Dictionary<int, GcellGtrx>();

dictionary.Add(gcell.CellId, gcellGtrx);

Solution

  • To check duplicate key, you can use:

    dictionary.ContainsKey(gcell.CellId);
    

    and to check duplicate value, you can use:

    dictionary.ContainsValue(gcellGtrx);