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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T16:39:23+00:00 2026-05-26T16:39:23+00:00

I try to write a program where Dictionary is indexed by List. (trust me

  • 0

I try to write a program where Dictionary is indexed by List. (trust me i do, and yes there are option, but i like indexing by list). There is a minimal working (actually not working, only one last line which is a problem) example:

using System;
using System.Collections.Generic;

namespace test
{
    class Program
    {
    static void Main(string[] args)
    {
        Dictionary<List<String>, int> h = new Dictionary<List<string>,int>();

        List<String> w = new List<string> {"a"};
        h.Add(w, 1);

        w = new List<string>{"b"};
        h.Add(w,2);

        w = new List<string>{"a"};

        int value = 0;
        h.TryGetValue(w, out value);
        Console.WriteLine(value+" "+h[w]);
    }
}

if one debugs this program, he will clearly see that there two elements in h, but still these elements are not accessible via correct indexes — h[w]. Am I wrong or is there something weird going on?

  • 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-26T16:39:23+00:00Added an answer on May 26, 2026 at 4:39 pm

    The problem with your app extends from the fact that:

    new List<String> { "a" } != new List<String> { "a" }
    

    Equality for lists checks to see if the two references refer to the same instance. In this case, they don’t. You’ve instead created two Lists with the same elements…which doesn’t make them equal.

    You can fix the problem by creating a custom Equality Comparer:

    public class ListEqualityComparer<T> : IEqualityComparer<List<T>>
    {
        public bool Equals(List<T> list1, List<T> list2)
        {
            return list1.SequenceEquals(list2);
        }
    
        public int GetHashCode(List<T> list)
        {
            if(list != null && list.Length > 0)
            {
                var hashcode = list[0].GetHashCode();
                for(var i = 1; i <= list.Length; i++)
                    hashcode ^= list[i].GetHashCode();
    
                return hashcode;
            }
    
            return 0;
        }
    }
    

    And then passing that to the Dictionary constructor:

    Dictionary<List<String>, int> h = 
        new Dictionary<List<string>,int>(new ListEqualityComparer<String>());
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I try to write to a large file, but it seems like it does
I try to write :ab in Vim for faster coding but the question is
There are many ways to write a Python program that computes a histogram. By
When writing simulations my buddy says he likes to try to write the program
I'm thinking of using Java to write a program that I might try to
I'm looking to try and write a chess AI. Is there something i can
I would like to write a program to receive some data using tcpClient from
This snippet works well if I try to write in a user directory but
I am trying to write a program using python-fuse, but I can't get file
I try to write a program that shows an additional (task)bar on the top

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.