-1

I am looking to create a custom string.GetHashCode() method for some of the strings used in a program namespace (as recommended on the string.GetHashCode() msdn page here) Unfortunately the string class is inheritance sealed.

The closest I have gotten is a base class with a string-typed Value accessor, but that will be a little annoying in that the code will read:

CustomStringTypeInstnce.Value.GetHashCode();

instead of the nicer:

CustomStringTypeInstnce.GetHashCode();

that would be possible if the string class were not inheritance sealed. Is the code read I am looking for at all possible?

if not,.. I don't necessarily need to or not need to override the base class string.GetHashCode(), for the entire program namespace but that would work too, how would that be done?

(EDIT) Sorry, got a little attention-tracked, yes, could just use

CustomStringTypeInstnce.GetHashCode();

as suggested by AppDeveloper's answer, I was/am also looking to be able to assign a string to the wrapped string variable directly without having to use an accessor, but I guess that pretty much means using the prohibited inheritance.

stackuser83
  • 2,012
  • 1
  • 24
  • 41
  • Are you using polymorphism at all? Is there no reason you cannot just create your own `NewGetHashCode()` for your custom class and just call it whereever you need it? – David Pilkington Dec 08 '13 at 18:43
  • What recommendation are you referring to from "(as recommended on the string.GetHashCode() msdn page here)"? I don't see anything in there which would lead me to think I should roll my own hash code generation routine for strings in a "program namespace". Only that I shouldn't really depend on what values are used; that is, I shouldn't record the hashcodes and compare them from one version of .NET to the other. – Chris Sinclair Dec 08 '13 at 18:46
  • 2
    Why do you need that? I.e. most collection/sorting classes and methods take custom comparers - so instead of overriding GetHashCode you can provide custom implementation without inheritance... – Alexei Levenkov Dec 08 '13 at 18:48
  • Why you need to override GetHashCode for even a part of the program namespace. – paparazzo Dec 08 '13 at 18:58
  • 1
    @skepticalstackposters The override recommendation is in the .net 2.0 version of the msdn documentation for the method: http://msdn.microsoft.com/en-us/library/system.string.gethashcode(v=vs.80).aspx). – stackuser83 Dec 08 '13 at 19:01
  • @stackuser83: Your link appears to be broken, but you probably meant [this page](http://msdn.microsoft.com/en-us/library/system.string.gethashcode%28v=vs.80%29.aspx), which indeed says " If you require the behavior of GetHashCode to be constant, override the runtime implementation of GetHashCode with an implementation of your own that you know will never change." While that refers to a different case, in general, using a different than the default string hash code algorithm is only useful if you know you'll have strings that will create many collisions with the default algo. Is that the case? – O. R. Mapper Dec 08 '13 at 19:04
  • I am using the GetHashCode for a checksum of half a whole load of ftext, therefore expect to save some cycles when I know expectation too – stackuser83 Dec 08 '13 at 19:08

2 Answers2

1

You can't override String.GetHashCode because it is sealed.

Since the only good usage for GetHashCode is to use for "hash" data structure like Dictionary and HashSet you can instead use constructors that take different IEqualityComparer.

Most common case for strings are covered by StringComparer class. I.e. to have dictionary that compares string in cases insensitive way (ignore culture):

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

Note: using GetHashCode for any other purpose (like persisting information about a string instead of SHA256 or other well defined hash) is not recommended. String.GetHashCode documentation explicitly calls out that the results may change from version to version or even between different processes/app domains.

Hash codes for identical strings can differ across versions of the .NET Framework and across platforms (such as 32-bit and 64-bit) for a single version of the .NET Framework. In some cases, they can even differ by application domain

Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
0

i dont think, you can!

String class is sealed for a reason to disallow any such modification.

You can do, expose create Class like

namespace ConsoleApplication2
{

    class MyClass
    {
        private string _state;
        public override int GetHashCode()
        {

            return _state.MyCustomHasCode();
        }
    }
    public static class Program
    {
        public static int MyCustomHasCode(this string str)
        {
            // return "your own hashcode implementation
        }
        static void Main(string[] args)
        {

        }
    }
}
Parimal Raj
  • 20,189
  • 9
  • 73
  • 110