5 ms·
That does answer an unanswered question I had on SO about string hashing. If strings are immutable, why isn't the hash code memoized? Seems like it would make H
by DoggettCK 12y ago
That does answer an unanswered question I had on SO about string hashing. If strings are immutable, why isn't the hash code memoized? Seems like it would make HashSet/Dictionary lookups using string keys much faster.
- munificent 12y agoPresumably because it's not worth the memory hit to store the hash.
- DoggettCK 12y agoThat was my assumption, too. They do memoize the length, but I'm sure those bytes add up, having run into OutOfMemoryExceptions building huge amounts of strings before.
- reddiric 12y agoStrings know their length in the CLR because they are represented as BSTRs http://blogs.msdn.com/b/ericlippert/archive/2011/07/19/strings-immutability-and-persistence.aspx http://blogs.msdn.com/b/ericlippert/archive/2011/07/19/strin... This lets them interoperate with OLE Automation.
- porges 12y agoYou have to store the length, C# strings can contain '\0' (although the hashing code doesn't take this into account!)