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

  • Home
  • SEARCH
  • 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 6191341
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T02:44:47+00:00 2026-05-24T02:44:47+00:00

I’m wondering is there a way to overwrite Hashtable (or Dictionary) class so it

  • 0

I’m wondering is there a way to overwrite Hashtable (or Dictionary) class so it would automatically do boxing/unboxing operations on objects. In other words:

myHashtable["value1"] = "this_is_string";
myHashtable["value2"] = 123;

string a = myHashtable["value1"];
int b = myHashtable["value2"];
// errors as expected, since i need to cast it to specific type from object

And apparently C# doesn’t allow overwriting public T this[object key] operator with different types, since I tried to do something like this:

public int this[object key] { get { return (base[key] as int); } set {} } // etc
public string this[object key] { get { return (base[key] as string); } set {} } // etc
// error

Any ideas or tips what is the simplest way (if any) to avoid casting while using associative arrays in C# (there’s no need to strictly use Hashtable)? And if there’s no way to do it, I would appreciate if someone more knowledgeable than me, explain why it is so and what are the fundamentals behind it.

Thank you.

Edit:
The reason I need it, is that I’m creating a custom Settings class. Settings might have different types of values, such as let’s say “HowManyItemsToDisplay” would have some integer value, while “NameOfSomeControl” would be a string. Therefore it would be nice to avoid any casting when writing something like:

myControl.Text = MySettings["SomeTextValue"];

or

while (MySettings["SomeIntValue"] > 0) { .. }
  • 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-24T02:44:48+00:00Added an answer on May 24, 2026 at 2:44 am

    Assuming you have a good reason for doing this you could write an extension method to give you a reasonable way simulate that:

    myHashTable.GetAs<string>("value1");
    myHashTable.GetAs<int>("value2");
    
    public static T GetAs<T>( this Hashtable ht, object key )
    {
        return (T)ht[key];
    }
    

    If you know the key is always a string you can make that the parameter type for the key and even use the generic dictionary or other structure with a strongly typed key.

    And to be perfectly clear the casting and boxing are still there, just hidden from view.

    Additional response to edit:

    If you are able to use C# 4 I think you can pretty much get want you want syntactically with Dictionary<object,dynamic>. Of course, if you are able to do that then you might just want to make the settings object dynamic and use the syntax mySettings.Value1 instead.

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have text I am displaying in SIlverlight that is coming from a CMS
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string
this is what i have right now Drawing an RSS feed into the php,
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but

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.