Is there a way to make .NET Core GetHashCode deterministic

后端 未结 2 952
长情又很酷
长情又很酷 2021-01-02 01:10

I just wasted few hours of debugging before realizing that contrary to .NET, in .NET Core, GetHashCode returns a different value every time you run your code. I totally unde

2条回答
  •  遥遥无期
    2021-01-02 01:50

    According to docs only change of framework should change the hash result. I too stumbled upon this. My solution was to create my own hash algorithm. It just took a few minutes as I didn't need anything fancy.

    private static int GetSimpleHash(string s)
    {
        return s.Select(a => (int)a).Sum();
    }
    

    On a side note I have filed a bug for dotnet core 1.1. (string).GetHashCode() here.

    Update

    The hash can change due to framework or domain. This means that two subsequent runs of the same program can return different results.
    Alas my bug report is moot and changed to a documentation update instead.

    Update update

    Documentation is updated to be a little bit more clear.

提交回复
热议问题