16925

Case insensitive string for use in dictionary?

I need a dictionary to have a key that is a string but ignores case. I've decompiled the Dictionary type and it basically creates a hash table of the key's hash code. I can't subclass string because its a primitive type so I've created my own class to use as a key:

struct StringCaseInsensitiveHash { private readonly string _innerString; public StringCaseInsensitiveHash(string str) { _innerString = str; } public static implicit operator string(StringCaseInsensitiveHash stringCaseInsensitiveHash) { return stringCaseInsensitiveHash._innerString; } public override int GetHashCode() { return _innerString.ToLower().GetHashCode(); } }

Is there a better way to do this though?

Thanks,

Answer1:

Dictionary constructor allows you to pass IEqualityComparer which it will use to compare the keys and for hashing purpose too.

You can use StringComparer.OrdinalIgnoreCase , StringComparer.CurrentCultureIgnoreCase or StringComparer.InvariantCultureIgnoreCase depends upon your need.

More info available in MSDN

var myDictionary = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);

Recommend

  • How to access COM objects from different apartment models?
  • Cannot run nunit tests with Nant
  • WPF and background worker and the calling thread must be STA
  • Docker container doesn't start, showing as 'Exited n seconds ago'
  • C# OpenFileDialog Thread start but dialog not shown
  • How can I convert this tuple of tuples into a count of its elements?
  • Is it OK to write code after [super dealloc]?
  • Gson serializing Spring beans
  • how to save bool value in KeychainItemWrapper
  • Multiple sockets for clients to connect to
  • Complex trait requirements on struct
  • c++ using primitive types as a base class
  • C function strchr - How to calculate the position of the character?
  • Scala: Function returning an unknown type
  • Using same constraints in multiple classes
  • LiveData is abstract android
  • Monotouch crashes with NullReferenceException on non nullable object
  • sweetalert2 inputoptions from file in select example
  • Simple linked list-C
  • Android changing fragment order inside FragmentPagerAdapter
  • how does System.Web.HttpRequest::PathInfo work?
  • Not able to aggregate on nested fields in elasticsearch
  • Parse a date string in a specific locale (not timezone!)
  • OOP Javascript - Is “get property” method necessary?
  • Caching attributes in superclass
  • Incrementing object id automatically JS constructor (static method and variable)
  • Validaiting emails with Net.Mail MailAddress
  • How to handle AllServersUnavailable Exception
  • sending/ receiving email in Java
  • 0x202A in filename: Why?
  • Cannot Parse HTML Data Using Android / JSOUP
  • Free memory of cv::Mat loaded using FileStorage API
  • JTable with a ScrollPane misbehaving
  • Java static initializers and reflection
  • unknown Exception android
  • Observable and ngFor in Angular 2
  • failed to connect to specific WiFi in android programmatically
  • Unable to use reactive element in my shiny app
  • Python/Django TangoWithDjango Models and Databases
  • How can I use threading to 'tick' a timer to be accessed by other threads?