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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T16:43:25+00:00 2026-05-20T16:43:25+00:00

I use dictionary in my program like this Dictionary<string, List<Term>> which Term is object

  • 0

I use dictionary in my program like this

Dictionary<string, List<Term>>

which Term is object from class has two fields (name , id)
I need add new object into dictionary where the name of new object is not found in the dictionary…
if the field name in new object is existent as name of old object I will not add it..
can you help me
the code I use is

 foreach (string term in terms)
    {
        if (!dictionary.ContainsKey(term))
        {
            dictionary.Add(term, new List<Term>());
        }

        Term TT=new Term(i,name);
        if (!dictionary[term].Contains(TT))
        {
            dictionary[term].Add(TT);


        }

this code dosn’t work exactly..

  • 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-05-20T16:43:25+00:00Added an answer on May 20, 2026 at 4:43 pm

    The problem is here:

        if (!dictionary[term].Contains(TT))
        {
            dictionary[term].Add(TT);
        }
    

    The default behavior of List.Contains if class Term does not implement IEquatable<Term> is to check for reference equality. Since the object you just constructed cannot already be in the list, Contains will always return false and the new Term will always be added.

    One good solution would be to implement IEquatable<Term> in Term and specify the criteria you want for equality in the IEquatable<Term>.Equals method.

    Another solution (which is probably less desirable because it will only help this particular piece of code to work) is to change the test to

        // Change the t.name == TT.name test to whatever your idea
        // of "term X equals term Y" is
        if (dictionary[term].FindIndex(t => t.name == TT.name) == -1)
        {
            dictionary[term].Add(TT);
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The program was working with this implementation: class Instrument { public string ClassCode {
I often use Dictionary in C#2.0 with the first key as string that was
I would like to use Dictionary as TKey in another Dictionary . Something similar
I'd like to know what you think about this part of our program is
I want to make a C# Dictionary in which the key is the string
I've found this dictionary by William Whitaker on the Internet and I like its
I’m trying to implement a dictionary for use with WCF. My requirements are: actual
I'm want to use either a hash table or a dictionary in my access
I am using LINQ to query a generic dictionary and then use the result
I have a python module that makes use of a huge dictionary global variable,

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.