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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T05:41:06+00:00 2026-05-13T05:41:06+00:00

Basically, I want something like this: Dictionary<object, string> dict = new Dictionary<object, string>(); dict.Add(null,

  • 0

Basically, I want something like this:

Dictionary<object, string> dict = new Dictionary<object, string>();
dict.Add(null, "Nothing");
dict.Add(1, "One");

Are there any built into the base class library that allow this? The preceding code will throw an exception at runtime when adding the null key.

  • 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-13T05:41:06+00:00Added an answer on May 13, 2026 at 5:41 am

    You could avoid using null and create a special singleton value class that does the same thing. For example:

    public sealed class Nothing
    { 
      public static readonly Nothing Value = new Nothing(); 
      private Nothing() {}
    }
    
    Dictionary<object, string> dict = new Dictionary<object, string>();
    dict.add(Nothing.Value, "Nothing");
    dict.add(1, "One");
    

    This approach will fail to work if you intend to make your collection more strongly typed – let’s say for example you want the key to be a string. Since string is sealed you can’t inherit from it to create a “special value” substitute for null. Your alternatives become a bit more complicated. You could:

    1. Create some special constant value to represent the “empty” / “null” case. Kind of hacky and definitely a path to confusion. This can be a viable approach if the dictionary is completely private to some implementation class and you can write some Encode/Decode utility methods to avoid spreading the knowledge of how you translate keys all over the place.
    2. Create your own implementation of IDictionary that internally delegates to a Dictionary<> instance – except for the case of null. This violates the documented expectations for the IDictionary<> interface which does say that null keys should throw an exception. But you may be able to get away with it if it’s the only way to solve your real problem. This only works if you own and create the dictionary instance.
    3. Find a way to solve your problem without storing a “null” key in the dictionary. For example, consider not populating the null key in the dictionary and having some special case logic to deal with it. Keys have to be hashable and comparable to work with the underlying implementation, which is why null is prohibited normally.

    As an aside, does your dictionary key really need the key to be object? This can lead to subtle bugs due to reference equality being used where you may have intended Equals() to be evaluated as the basis for comparison.

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

Sidebar

Related Questions

Basically, I want to do something like this (in Python, or similar imperative languages):
I'm a bit stuck on this. Basically I want to do something like the
Basically I want to create one large object of many object in JavaScript. Something
So, I basically want to do something like this: http://jsfiddle.net/bUV6S/1/ ... But I want
I want to do something like this: const MyFirstConstArray: array[0..1] of string = ('Hi',
Im trying to do something like this . Basically i want to: When you
Basically I want something like this. What are the ready-made options for me? Is
Basically I want to do something like this: $(document).ready(function() { if ($(body).attr(class) === HomePage)
Is something like this possible? Namespace Transaction, Document Class Signer Public Sub New() 'Do
Okay what I want to do is something like this. |--> Child 1 (Parent=Root

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.