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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T04:34:27+00:00 2026-06-14T04:34:27+00:00

I have an application that use managed dlls. One of those dlls return a

  • 0

I have an application that use managed dlls. One of those dlls return a generic dictionary:

Dictionary<string, int> MyDictionary;  

The dictionary contains keys with upper and lower case.

On another side I am getting a list of potential keys (string) however I cannot guarantee the case. I am trying to get the value in the dictionary using the keys. But of course the following will fail since I have a case mismatch:

bool Success = MyDictionary.TryGetValue( MyIndex, out TheValue );  

I was hoping the TryGetValue would have an ignore case flag like mentioned in the MSDN doc, but it seems this is not valid for generic dictionaries.

Is there a way to get the value of that dictionary ignoring the key case?
Is there a better workaround than creating a new copy of the dictionary with the proper StringComparer.OrdinalIgnoreCase parameter?

  • 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-14T04:34:28+00:00Added an answer on June 14, 2026 at 4:34 am

    There’s no way to specify a StringComparer at the point where you try to get a value. If you think about it, "foo".GetHashCode() and "FOO".GetHashCode() are totally different so there’s no reasonable way you could implement a case-insensitive get on a case-sensitive hash map.

    You can, however, create a case-insensitive dictionary in the first place using:-

    var comparer = StringComparer.OrdinalIgnoreCase;
    var caseInsensitiveDictionary = new Dictionary<string, int>(comparer);
    

    Or create a new case-insensitive dictionary with the contents of an existing case-sensitive dictionary (if you’re sure there are no case collisions):-

    var oldDictionary = ...;
    var comparer = StringComparer.OrdinalIgnoreCase;
    var newDictionary = new Dictionary<string, int>(oldDictionary, comparer);
    

    This new dictionary then uses the GetHashCode() implementation on StringComparer.OrdinalIgnoreCase so comparer.GetHashCode("foo") and comparer.GetHashcode("FOO") give you the same value.

    Alternately, if there are only a few elements in the dictionary, and/or you only need to lookup once or twice, you can treat the original dictionary as an IEnumerable<KeyValuePair<TKey, TValue>> and just iterate over it:-

    var myKey = ...;
    var myDictionary = ...;
    var comparer = StringComparer.OrdinalIgnoreCase;
    var value = myDictionary.FirstOrDefault(x => String.Equals(x.Key, myKey, comparer)).Value;
    

    Or if you prefer, without the LINQ:-

    var myKey = ...;
    var myDictionary = ...;
    var comparer = StringComparer.OrdinalIgnoreCase;
    int? value;
    foreach (var element in myDictionary)
    {
      if (String.Equals(element.Key, myKey, comparer))
      {
        value = element.Value;
        break;
      }
    }
    

    This saves you the cost of creating a new data structure, but in return the cost of a lookup is O(n) instead of O(1).

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

Sidebar

Related Questions

I have a Web Application that use Hibernate 3.0. When I restart my database
I have C# application that makes use of some C libaries(which I have written
I have an application that makes use of frequently updated lists. So for example,
I have a reference application that I use to work through DDD issues, and
I have a Silverlight application that I use the Beta2 T4 Self tracking entities
I have an C# form application that use an access database. This application works
I have a WinForms application that makes use of a TaskDialog library that leverages
I have a web application that you can use to import information from another
I have a Java application that makes heavy use of a large file, to
I have a multithreaded application that makes heavy use of OpenSSL in C. It

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.