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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T01:53:37+00:00 2026-06-10T01:53:37+00:00

I created a sort of string wrapper class and want to use its instances

  • 0

I created a sort of string wrapper class and want to use its instances as dictionary keys interchangeably with usual strings. I overrode GetHashCode and Equals and got results that seem strange. I’ve isolated the issue. Please look at my code and explain me why does second lookup returns null.

void Main()
{
    var foo = new StringWrapper("foo");
    var h = new Hashtable {{ foo, "bar" }};
    Console.WriteLine(h["foo"]);
    Console.WriteLine(h[foo]); // null ??
}

public class StringWrapper
{
    readonly string wrapped;
    public StringWrapper(string s) {
        wrapped = s;
    }
    public override bool Equals(object obj) {
        return wrapped.Equals(obj);
    }
    public override int GetHashCode() {
        return wrapped.GetHashCode();
    }
    public override string ToString() {
        return wrapped;
    }
}
  • 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-10T01:53:38+00:00Added an answer on June 10, 2026 at 1:53 am

    Your equality implementation isn’t symmetric, nor reflexive:

    StringWrapper wrapper = new StringWrapper("foo");
    
    Console.WriteLine(wrapper.Equals(wrapper)); // False
    Console.WriteLine(wrapper.Equals("foo")); // True
    Console.WriteLine("foo".Equals(wrapper)); // False
    

    So you’re violating the rules specified in the docs for Object.Equals. Hashtable (and other classes) expect you not to violate those rules, and won’t work properly when you violate them.

    It sounds like you need a custom collection, rather than a wrapper which tries to pretend that the wrapper is equal to the original value.

    (As an aside, why are you still using non-generic Hashtable rather than Dictionary<,>?)

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

Sidebar

Related Questions

I've created a class called SpecialArray and I'd like to customize what sort of
I have created a custom Comparator to sort an ArrayList of Strings. I have
I have created the following string field for indexing. <fieldType name=short_text_for_sort class=solr.StrField omitNorms=true sortMissingLast=true
I created a python .py with some sort methods. I'm using numpy so that
I've created a javafx project in netbeans. It compiles fine, code completion works...sort off..
Say I want to create a sort of Pre-processor for existing java code, so
I want to create an animation effects which sort of shakes an image on
I want to create an Activity which shows a sort of menu a user
I've created a groovlet that will act as a sort of HTTP proxy. It
I've created 2 objects: public class Set { public Set() { _sorts = new

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.