Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7975625
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T08:41:39+00:00 2026-06-04T08:41:39+00:00

I have the following object and I want a dictionary to conditionally determine if

  • 0

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();
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T08:41:40+00:00Added an answer on June 4, 2026 at 8:41 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hi I have the following object: Hashtable<Object, Double> and I want to find the
In C# if I have the following object: IEnumerable<Product> products; and if I want
I have date in sting object in following format. dd/mm/yyyy I want to change
I have the following loop over a dictionary type collection foreach(KeyValuePair<Vector2, Object> entry in
I have a dictionary<String,Object> and I want to convert it to a List<Customer> Is
I have the following class that I want to bind it's dictionary's value to
I have the following Dictionary<> object: Dictionary<String, object> parameters = new Dictionary<string, object>(); parameters.Add(username,
I have a dictionary object, and I want to extract a subset of the
I have data on following variables. CryptoStream searialNumber = encryptedSearialNumber; Dictionary<string, object> x= userInput;
I have a NSMutableArray called putNumberUsed. It contains the following objects @blah1,@blah2,@blah3,@blah4. I want

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.