标签: gethashcode

是否有.Net 1.1兼容的String.GetHashCode在.Net 2.0代码中实现?

我有一个现有的应用程序,我错误地使用String.GetHashCode并将其持久化到磁盘.现在我正在将应用程序升级到.Net 2.0,我发现这个决定已经回来让我陷入困境.

我很想知道是否有人知道.Net 1.1兼容字符串散列算法的.Net 2.0实现.

显然,最好的解决方案是给自己买一台时间机器,然后回到2002年,即使考虑使用这种方式使用哈希码,也要自己开始.由于这似乎不太可能,我正在寻找一种解决方法.我现有的用户在他们的系统上有这些数据,所以我不可能对哈希或类似的东西进行一次大的转换.

随着更改为2.0,我将更新代码,因此它当然使用MD5或SHA.

我考虑从Mono中提取String.GetHashCode源,但由于Mono是GPL而且我的应用程序是商业用途,实际上不是一个选项.我甚至不知道Mono实现是否与MS .Net实现兼容,因为GetHashCode的合同不要求它兼容.

有任何想法吗?

.net-1.1 gethashcode .net-2.0

3
推荐指数
1
解决办法
420
查看次数

使用引用类型的值作为字典键必须做什么?

假设我有一个类T,我想用作Dictionary<T,U>集合中的键.

我必须实现什么才能T使这些键基于值T而不是T引用?

我希望它只是GetHashCode().

c# generics dictionary gethashcode

3
推荐指数
1
解决办法
132
查看次数

实现了GetHashCode但是Dictionary无法找到密钥?

在我的班上,我实施了EqualsGetHashCode.但当我在C#代码中使用它作为字典的键时,我收到错误:"Key not found exception" 谢谢,

public class Time: IEquatable<Time>
{
    public String hour;
    public String minute;

    public Time()
    {
        hour = "00";
        minute = "00";
    }

    public Time(String hour, String minute)
        : this()
    {
        this.hour = hour;
        this.minute = minute;
    }

    public override int GetHashCode()
    {
        int hash = int.Parse(hour) * 60 + int.Parse(minute);
        return hash.GetHashCode();
    }
    public override bool Equals(Time time)
    {

            return (this.hour == time.hour && this.minute == time.minute);

    }

}
Run Code Online (Sandbox Code Playgroud)

以及我使用它的代码:

Dictionary<Time, …
Run Code Online (Sandbox Code Playgroud)

c# dictionary iequatable gethashcode

3
推荐指数
1
解决办法
1267
查看次数

我的List <T> .Distinct()有什么问题?

我有一个实现IEqualityComparer的类MyItems并覆盖以下方法:

public bool Equals(MyItems item1, MyItems item2)
{
    return (item1.ID == item2.ID && item1.itemName.Equals(item2));
}
public int GetHashCode(MyItems item)
{
    return item.ID.GetHashCode() ^ item.itemName.GetHashCode();
}
Run Code Online (Sandbox Code Playgroud)

首先,为什么有GetHashCode必要?我理解压倒这种Equals方法,然而,GetHashCode我的必要性已经无法实现.

其次,这似乎不起作用.我有什么问题吗?在那里我不明白GetHashCode,那可能是我绊倒的地方.

c# wpf overriding gethashcode

3
推荐指数
2
解决办法
146
查看次数

如何从gethashcode生成的int中获取原始字符串

我已经生成了一个哈希码

string textBoxVal="Naresh";
int code =textBoxVal.GetHashCode();
textBox2.Text=code.ToString();
Run Code Online (Sandbox Code Playgroud)

它生成了一个整数值-1078339947;

现在我想用这个(-1078339947)哈希码获得原始名称Naresh.我怎样才能做到这一点.

c# gethashcode

3
推荐指数
1
解决办法
2900
查看次数

如何为独立于顺序的对象集合生成唯一的散列

假设我有一堂课

public class MyClass
{
    public string Type { get; set; }
    public int Id { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

我有一个集合类,它只是一个强类型列表

public class MyClassList : List<MyClass>
{
    public MyClassList(IEnumerable<MyClass> enumerable) : base (enumerable) {}
}
Run Code Online (Sandbox Code Playgroud)

我希望MyClassList能够根据内容生成唯一的哈希码MyClassList。的哈希码MyClass应该基于这两个属性。即使对象的顺序不同,的哈希码也MyClassList应该相同

为了处理排序问题,我想我可以在生成哈希码之前对列表进行排序,但我不确定如何生成列表的哈希码。

.net c# collections equality gethashcode

3
推荐指数
1
解决办法
7783
查看次数

实现正确的GetHashCode

我有以下课程

public class ResourceInfo
{
    public string Id { get; set; }
    public string Url { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

其中包含有关某些资源的信息.现在我需要通过以下方案检查两个这样的资源是否相等(我实现了IEquatable接口)

public class ResourceInfo : IEquatable<ResourceInfo>
{
    public string Id { get; set; }
    public string Url { get; set; }

    public bool Equals(ResourceInfo other)
    {
        if (other == null)
            return false;

        // Try to match by Id
        if (!string.IsNullOrEmpty(Id) && !string.IsNullOrEmpty(other.Id))
        {
            return string.Equals(Id, other.Id, StringComparison.InvariantCultureIgnoreCase); 
        }

        // Match by Url if can`t match by Id
        return string.Equals(Url, other.Url, …
Run Code Online (Sandbox Code Playgroud)

c# equality gethashcode

3
推荐指数
1
解决办法
376
查看次数

如何在F#中为一个受歧视的联盟覆盖GetHashCode和CompareTo?

我有一个简单的F#区分联合,它结合了bool,一个字符串和一个浮点数.我想覆盖此联合的Object.Equals(arg),以便在检查浮点相等时我可以放入epsilon来解决精度错误.编译器抱怨说如果我覆盖this.Equals(arg),我也应该重写this.GetHashCode()和this.CompareTo(arg).对于这些覆盖,我没有计划特殊功能,所以我只想调用这些方法的默认版本.在我的实现中,我有三次调用GetHashCode,并且对我所区分的并集中的每种类型调用CompareTo三次:每种类型一次.

有没有办法只用一次GetHashCode调用来编码GetHashCode覆盖?CompareTo的问题是什么?我所区分的联盟中的所有类型都实现了ICompareable.

[<CustomEquality;CustomComparison>]
type MyType =
    | Bool of bool
    | Str of string
    | Float of float

    override this.Equals(arg) =
        let epsilon = 0.1
        match arg with
        | :? MyType as other ->
            match this, other with
            | Bool(a), Bool(b) -> a = b
            | Str(a), Str(b) -> a = b
            | Float(a), Float(b) -> Math.Abs(a - b) < epsilon
            | _ -> false
        | _ -> false

    override this.GetHashCode() =
        match this with
        // Three calls to GetHashCode. …
Run Code Online (Sandbox Code Playgroud)

f# icomparable gethashcode discriminated-union

3
推荐指数
1
解决办法
608
查看次数

哈希码是否存储在SyncBlockIndex / SyncBlock中

.net中的每个对象都有标题(SyncBlockIndexMethodTablePointer),如果没有链接到该对象的SyncBlock GetHashCode()SyncBlockIndex则在您调用该结果时可以将其保存在其中,如果有,则可以将其保存在SyncBlock中。

当我们不重写GetHashCode方法时,哈希码是对象存在期间的保存,但是如果我们返回动态哈希码(取决于对象的状态),哈希码将如何存储?

我知道哈希码在对象存在期间应该是相同的,提出问题的目的是了解哈希码如何存储在SyncBlockIndex或SyncBlock中。

.net c# gethashcode

3
推荐指数
1
解决办法
158
查看次数

Double.GetHashCode算法或覆盖

我有一个应用程序项目,管理和非托管代码都运行,我需要使用相同的算法在两个系统中散列双值.所以要么我将覆盖System.Double.GetHashCode()或在c ++代码中使用其算法.我找不到double.gethashcode算法并决定覆盖该函数.但我有一个奇怪的错误.

无法将类型double隐式转换为System.Double

这是代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace System
{
  public struct Double
  {
    unsafe public override int GetHashCode()
    {
      fixed (Double* dd = &this)
      {
        int* xx = (int*)dd;
        return xx[0] ^ xx[1] ;
      }

    }
  }
}

namespace ConsoleApplication1
{
  class Program
  {
    static void Main(string[] args)
    {
      double dd = 123.3444; // line 1
      // Double dd = 123.3444; // line 2
      // Cannot implicitly convert type double to System.Double
      Console.WriteLine(dd.GetHashCode());

      Console.ReadLine(); …
Run Code Online (Sandbox Code Playgroud)

c# algorithm double gethashcode

2
推荐指数
2
解决办法
4758
查看次数