当字典中的键为对象时,直接用ContainsKey不可以,当向字典中增加的时候,有同样的对象但还是可以加进去,是不是要重写什么东西???具体如何实现!!!???

解决方案 »

  1.   

    对象按地址引用的,要进行比较,你需要重写 Equals
    可参考
    http://blog.csdn.net/aladdinty/article/details/3400837
      

  2.   

    如果是同一对象可直接Equals
    但如果是不同的对象,但值一样与必须重写 Equals 
      

  3.   

    需要重写 Equals 和 GetHashCode 两个方法
      

  4.   

    /// <summary>
        /// A result is a search result which contains a post and its ranking.
        /// </summary>
        internal class Result : IComparable<Result>
        {
            #region Constants and Fields        /// <summary>
            ///     The post of the result.
            /// </summary>
            internal IPublishable Item;        /// <summary>
            ///     The rank of the post based on the search term. The higher the rank, the higher the post is in the result set.
            /// </summary>
            internal int Rank;        #endregion        #region Public Methods        /// <summary>
            /// Returns a hash code for this instance.
            /// </summary>
            /// <returns>
            /// A hash code for this instance, suitable for use in hashing algorithms and data structures like a hash table. 
            /// </returns>
            public override int GetHashCode()
            {
                return this.Item.Id.GetHashCode();
            }        #endregion        #region Implemented Interfaces        #region IComparable<Result>        /// <summary>
            /// Compares the current object with another object of the same type.
            /// </summary>
            /// <param name="other">
            /// An object to compare with this object.
            /// </param>
            /// <returns>
            /// A 32-bit signed integer that indicates the relative order of the objects being compared. The return value 
            ///     has the following meanings: Value Meaning Less than zero This object is less than the other parameter.Zero 
            ///     This object is equal to other. Greater than zero This object is greater than other.
            /// </returns>
            public int CompareTo(Result other)
            {
                return other.Rank.CompareTo(this.Rank);
            }        #endregion        #endregion
        }
    看看
      

  5.   


    if (dic.ContainsKey(_bsMod))//如果集合中包含此对象,直接返回Table如果我要像这样写,该怎么实现。。