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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T09:59:58+00:00 2026-05-15T09:59:58+00:00

We wanted to assign/add an item to a Dictionary using the key index, like

  • 0

We wanted to assign/add an item to a Dictionary using the key index, like this:

Dictionary<string, object> dict = new Dictionary<string, object>();
dict["key"] = dict["key"] ?? "object";

But it results to: “The given key was not present in the dictionary.“

Is there any way we could assign values to this dictionary the same as the way I assign values to sessions, using null coalesce:

Session["Member"] = Session["Member"] ?? new Member();

Thanks

Edit:
I actually tried to override the indexer from a child class of dictionary, but to no avail.
Implementing IDictionary interface would seem too much because we just need the functionality of the code below (i.e., return null if key is not found).
So I think, maybe we could use extensions like:

public static TValue [this Dictionary<TKey, TValue> thisDictionary, TKey key] {
    get { return thisDictionary.ContainsKey(key) ? thisDictionary[key] : null; }
    set { thisDictionary[key] = value; }
}

but it is not working or even compiling.

Any other simpler ways so we could reuse this indexer functionality? Thanks a lot.

  • 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-15T09:59:59+00:00Added an answer on May 15, 2026 at 9:59 am

    If you want to do this inline:

    dict["key"] = dict.ContainsKey("key") ? (dict["key"] ?? "object") : "object"; 
    

    Alternatively:

    dict["key"] = dict.ContainsKey("key") && dict["key"] == null ? dict["key"] : "object";
    

    Edit as per comments:

    public static TValue GetSafe(this Dictionary<TKey, TValue> thisDictionary, TKey key) 
    { 
       if (!thisDictionary.ContainsKey(key))
           thisDictionary[key] = null;
    
       return thisDictionary[key];
    }
    

    This allows you to do this:

    thisDictionary["randomKey"] = thisDictionary.GetSafe("randomKey") ?? "someValue";
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For instance, if dict['sample']: //append values to dict['sample'] else: // assign new key to
I wanted to write something basic in assembly under Windows. I'm using NASM, but
I just wanted to assign position.coords.latitude and longitude to lat and lon vars but
I have a file selector (which works as needed) but I wanted to add
I wanted opinion is it a good practice to do things like this while
Suppose I'm using R's interactive console, and I've just done something like this: long_running_command()
This is very closely related to this other question , but that question wanted
Wanted to see if anyone knows how to add props to a image with
I wanted to see what happened if you change the reference of an object
I wanted to capture whether a modifier key was being pressed during application startup

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.