1

I have the following object and I want a dictionary to conditionally determine if there is a duplicate. For example, in one dictionary I only care about two properties being unique for my key. In a second dictionary, I want all the properties being unique for the key.

Question 1:

What interfaces should I override to accomplish this? (e.g. GetHashCode, IEqualityComparer, equals operator)

Question 2:

What should I do if I change a property that ultimately changes the value of the key? This is probably more relevant if I do an Dictionary since .NET framwork somehow handles this for me, but I never thought about it.

Code

public class EventData : IEqualityComparer<EventData>
{
    public string ComputerName { get; set; }
    public Guid? CategoryName { get; set; }
    public string LogName { get; set; }
    public int EventID { get; set; }
    public long? EventUniqueTracker { get; set; }

    public DateTime LastQueryDate { get; set; }
    public DateTime? DateOfRecord { get; set; }

    //public int QueryCount { get; set; }
    public int QueryCount = 0 ;//

    public string  zData { get; set; }

    public EventData(string computerName, Guid? categoryName, string logName, int eventID, long? eventUniqueTracker, int queryCount)
    {
        ComputerName = computerName;
        CategoryName = categoryName;
        LogName = logName;
        EventID = eventID;
        EventUniqueTracker = eventUniqueTracker;

        LastQueryDate = DateTime.Now;
        QueryCount = queryCount;
    }

    public EventData()
    {
    }

    public override int GetHashCode()
    {
        return GetHashCode(HashType.ZCompCatLogEventAllData);
    }
    public object GetString(HashType hType)
    {
        switch (hType)
        {
            case HashType.AComputerName:
                return ComputerName;
                break;
            case HashType.BCompAndCat:
                return new { A = ComputerName, B = CategoryName };
                break;
            case HashType.CCompCatLog:
                return new { A = ComputerName, B = CategoryName, C = LogName };
                break;
            case HashType.DCompCatLogEvent:
                return new { A = ComputerName, B = CategoryName, C = LogName, D = EventID };
                break;
            case HashType.ECompCatLogEventUserDefined1:
            case HashType.FCompCatLogEventUserDefined2:
            case HashType.ZCompCatLogEventAllData:
                return new { A = ComputerName, B = CategoryName, C = LogName, D = EventID, E = EventUniqueTracker };
            default:
                break;
        }
        return new object { };
    }

    public int GetHashCode(HashType hType)
    {
        return GetString(hType).GetHashCode();
        return 1;
    }

    public override string ToString()
    {
        return ComputerName + " " + CategoryName + " " + LogName + " " + EventID + " " + EventUniqueTracker;
    }

    public bool Equals(EventData x, EventData y)
    {
        return x.ComputerName == y.ComputerName &&
               x.CategoryName == y.CategoryName &&
               x.LogName == y.LogName &&
               x.EventID == y.EventID &&
               x.EventUniqueTracker == y.EventUniqueTracker;
    }

    public int GetHashCode(EventData obj)
    {
        EventData ci = (EventData)obj;
        // http://stackoverflow.com/a/263416/328397
        return new { A = ci.ComputerName, B = ci.CategoryName, C = ci.LogName, D = ci.EventID, E = ci.EventUniqueTracker }.GetHashCode();
    }
}
makerofthings7
  • 60,103
  • 53
  • 215
  • 448

1 Answers1

2

It sounds like you should be implementing IEqualityComparer<EventData> - but not within EventData itself. Create two separate implementations - one for the first notion of equality, and one for the second. Then create your dictionaries as:

var first = new Dictionary<EventData, string>(new PartialDataEqualityComparer());
var second = new Dictionary<EventData, string>(new FullDataEqualityComparer());

Or perhaps you want to treat the second case as the "natural" equality for EventData, in which case you could make EventData implement IEquatable<EventData> and not specify a comparer when creating the second dictionary.

Basically, you implement IEquatable<T> to say "an instance of this type is capable of comparing itself against an instance of T" whereas you implement IEqualityComparer<T> to say "an instance of this type is capable of comparing any two instances of T".

What should I do if I change a property that ultimately changes the value of the key?

You're stuffed, basically. You won't (or at least probably won't) be able to find that key again in your dictionary. You should avoid this as carefully as you possibly can. Personally I usually find that classes which are good candidates for dictionary keys are also good candidates for immutability.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • How does `GetHashCode()` relate in this conversation? Of these interfaces, which ones matter with Dictionary, or ConcurrentDictionary? – makerofthings7 May 24 '12 at 01:07
  • hmm... I think I could accomplish this by making the mutable types classes, and inherit to my main class. I wonder which overload of IEquatable will be visible when I cast to the more general class – makerofthings7 May 24 '12 at 05:49
  • @makerofthings7: Both IEqualityComparer and IEquatable are relevant to both Dictionary and ConcurrentDictionary. It's not clear *which* GetHashCode method you're talking about - the one declared by the object won't be used if a separate comparer is specified. I would personally try to avoid using inheritance in conjunction with equality - it ends up being confusing and difficult to implement correctly. – Jon Skeet May 24 '12 at 05:55