【问题标题】:C# class definition HELPC# 类定义帮助
【发布时间】:2011-10-27 06:04:39
【问题描述】:

这是一个我似乎无法回答的项目问题

using System;

namespace ConsoleApplication2
{
    internal class Equipment : IComparable
    {
        private readonly string type;
        private readonly int serialNo;
        private string colour;
        public decimal cost;

        public Equipment(string type, int serialNo)
        {
            this.type = type == null ? "" : type.Trim();
            this.serialNo = serialNo;
        }

        public string Key
        {
            get { return type + ":" + serialNo; }
        }

        int IComparable.CompareTo(object obj)
        {
            return 0;
        }
    }
}

(a) 覆盖适当的方法以确保表示相同设备项的类的不同实例在系统中将被视为相同。

(b) 重写适当的方法以使此类的实例能够通过键存储(和查找)在哈希表中

【问题讨论】:

  • 关键是你要找的方法不是直接在这个类中找到的,而是在这个类继承自的类中找到的。

标签: c#


【解决方案1】:

手动编写GetHashCode 并不容易。无论如何,这是 ReSharper 为此目的生成的代码。这是一个完整的解决方案。 (它当然应该包含在你的类定义中)。但是,如果有人问你,你会怎么说——为什么以及如何工作?可能会很尴尬。

所以,除了GetHashCodeEquals,其他人建议您阅读,您还可以查找http://msdn.microsoft.com/en-us/library/system.object.referenceequals.aspxhttp://msdn.microsoft.com/en-us/library/a569z7k8(v=VS.100).aspx

至于 GetHashCode 中 397 背后的谜团,请在 StackOverflow 上查看这个问题:Why is '397' used for ReSharper GetHashCode override?

        public bool Equals(Equipment other)
        {
            if (ReferenceEquals(null, other))
            {
                return false;
            }
            if (ReferenceEquals(this, other))
            {
                return true;
            }
            return Equals(other.colour, colour) && other.cost == cost && other.serialNo == serialNo && Equals(other.type, type);
        }

        public override bool Equals(object obj)
        {
            if (ReferenceEquals(null, obj))
            {
                return false;
            }
            if (ReferenceEquals(this, obj))
            {
                return true;
            }
            if (obj.GetType() != typeof (Equipment))
            {
                return false;
            }
            return Equals((Equipment) obj);
        }

// note: if "Override the appropriate method to enable instances of this class
// to be stored (and found) by key in a hash table" is supposed to mean that only type and
// serialNo should be taken into account (since they are used to generate
// the Key value) - just remove the lines with cost and colour
        public override int GetHashCode()
        {
            unchecked
            {
                int result = (colour != null ? colour.GetHashCode() : 0);
                result = (result*397) ^ cost.GetHashCode();
                result = (result*397) ^ serialNo;
                result = (result*397) ^ (type != null ? type.GetHashCode() : 0);
                return result;
            }
        }

【讨论】:

    【解决方案2】:
    1. 用适当的比较逻辑覆盖Equals()
    2. 覆盖GetHashCode(),参见GetHashCode Guidelines in C#

    必须在执行此类任务之前开始阅读此内容 Why is it important to override GetHashCode when Equals method is overriden in C#?

    【讨论】:

      【解决方案3】:

      为此,您应该重写 Equals 和 GetHashCode 方法。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-12-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-08-07
        相关资源
        最近更新 更多