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 8269543
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T06:16:06+00:00 2026-06-08T06:16:06+00:00

my class: public class myClass { public int A { get; set; } public

  • 0

my class:

public class myClass
{
    public int A { get; set; }
    public int B { get; set; }
    public int C { get; set; }
    public int D { get; set; }
}

and main example:

Dictionary<myClass, List<string>> dict = new Dictionary<myClass, List<string>>();
myClass first = new myClass();
first.A = 2;
first.B = 3;

myClass second = new myClass();
second.A = 2;
second.B = 3;
second.C = 5;
second.D = 6;

dict.Add(first, new List<string>());

if (dict.ContainsKey(second))
{
    //
    //should come here and update List<string> for first (and only in this example) key 
    //
}
else
{
    //
    //if myFirst object has difference vlues of A or B properties
    //
    dict.Add(second, new List<string>());
}

How to do this?

  • 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-08T06:16:07+00:00Added an answer on June 8, 2026 at 6:16 am

    If you always want the dictionary only to compare on A and B, you have two options. Either use the constructor that implements IEqualityComparer<TKey> and put your comparison logic there, or have your class implement IEquateable<T> GetHashCode and Equals so the default comparer will give you the results you are looking for.

    If you only want to compare on A and B in your one situation you will need to use the .Keys property and the Linq extension method Contains that allows you to pass in a IEqualityComparer<T>. However, when doing it this way you loose the speed benefits of using a Dictionary, so use it sparingly.

    public class MyClassSpecialComparer : IEqualityComparer<myClass>
    {
        public bool Equals (myClass x, myClass y)
        { 
            return x.A == y.A && x.B == y.B 
        }
    
        public int GetHashCode(myClass x)
        {
           return x.A.GetHashCode() + x.B.GetHashCode();
        }
    
    
    }
    
    
     //Special case for when you only want it to compare this one time
     //NOTE: This will be much slower than a normal lookup.
        var myClassSpecialComparer = new MyClassSpecialComparer();
        Dictionary<myClass, List<string>> dict = new Dictionary<myClass, List<string>>();
        //(Snip)
        if (dict.Keys.Contains(second, myClassSpecialComparer ))
        {
            //
            //should come here and update List<string> for first (and only in this example) key 
            //
        }
    
     //If you want it to always compare
        Dictionary<myClass, List<string>> dict = new Dictionary<myClass, List<string>>(new MyClassSpecialComparer());
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Assuming public class MyClass { public int ID {get; set; } public string Name
Say I have this class: class myclass { public int Field1{ get; set; }
I have a class: public class MyClass<T> { public string TestProperty { get; set;
Given the following example class: public class MyClass { public string S { get;
Given this class: public class MyClass { public int MyProperty {get; set;} } How
I have an object model like this: public class MyClass { int TheProp {get;set;}
My class: public class Foo { public int A { get; set; } public
I have the following object: public class MyClass { public int Id { get;
I have class: public class MyClass { [XmlElement(Date)] public DateTime Date { get; set;
I have a class like this: class MyClass{ public: MyClass(int Mode); private: std::map <

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.