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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T17:08:26+00:00 2026-06-15T17:08:26+00:00

I’m writing an app in C#/.NET 4.5 and I have a dictionary defined like

  • 0

I’m writing an app in C#/.NET 4.5 and I have a dictionary defined like this:

Dictionary<int, string> d = new Dictionary<int, string>();

My integer keys are sequential and unique and, except for empty strings, my values will be unique as well. For example:

Key     Value
  0     AAAAAAAAAA
  1     BBBBBB
  2     (empty string)
  3     CCCCCCCCCCCCCCCCCCCCCCCC
  4     DDDDDDDDDDDDDDD
  5     EEEEEEEEEEEEEEEEEEE
  6     (empty string)

When I have a new string to add to the dictionary, I need to assign it to one of the keys which has a value of (empty string). Ideally, I’d like to do this at the first instance of such a key. I’m currently looping through my dictionary like this:

int keyNumber;

foreach (KeyValuePair<int, string> pair in d)
{
    if (pair.Value == string.Empty)
    {
        keyNumber = pair.Key;
        break;
    }
}

d[keyNumber] = "new string goes here!";

This works but is there a better (or faster) way to accomplish the same thing? If this actually is the best approach, is it possible to shorten it into a LINQ expression?

EDIT —————————————————————————–

I left out what was probably something important to explain. The values that get loaded into my dictionary are actually loaded from a binary file comprised of fixed-length byte arrays which hold ASCII strings. The file will always consist of 5000000 bytes or 100000 total strings @ 50 bytes each. The file structure was determined by some hardware limitations on the device that will consume the file. So the key for each KeyValuePair in my dictionary is actually used to determine the offset into the file where I need to write any changes when the user is finished making them. For example, using a a key of 4, I then multiply by 50 to a starting offset of 200 bytes. Then I write the modified string back at that location. Sorry for omitting that in my original post.

  • 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-15T17:08:27+00:00Added an answer on June 15, 2026 at 5:08 pm
    d[d.FirstOrDefault(x => string.IsNullOrEmpty(x.Value)).Key] = "My String";
    

    However, a dictionary is really meant to store true key-value pairs. A list sounds like it is better suited to your case where it seems like you only really need to store values. Even so, list elements are still accessible by index and you can include empty elements so there doesn’t (from what you have stated) seem to be a reason to stick with a dictionary.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
I've got a string that has curly quotes in it. I'd like to replace
I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I would like to count the length of a string with PHP. The string
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have an autohotkey script which looks up a word in a bilingual dictionary
This could be a duplicate question, but I have no idea what search terms

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.