2015-06-23 58 views
-1

我在使用Dictionary ContainsKey方法時遇到問題。儘管我重寫了EqualsGetHashCode方法,但我總是得到這個值。使用字典ContainKey總是返回true

我錯過了什麼?

FreeTime對象包含2個DateTime類型的變量(開始和結束)。

這裏是滿級

class FreeTime : IEquatable<FreeTime> 
{ 
    #region Constants 

    private const bool FirstDayOfWeekIsMonday = true; 

    #endregion 

    #region Private Variables 

    private DateTime start; 
    private DateTime end; 
    private static bool TodayIsSunday = (int)DateTime.Today.DayOfWeek == 0; 


    #endregion 

    #region Public Fields 

    public DateTime Start { get { return start; } } 

    public DateTime End { get { return end; } } 

    public void setStart(DateTime startValue) { start = startValue; } 

    public void setEnd(DateTime EndValue) { end = EndValue; } 

    #endregion 

    #region Constructor 

    public FreeTime() 
    { 
     start = DateTime.Today; 
     end = DateTime.Today; 
    } 

    public FreeTime(DateTime s,DateTime e) 
    { 
     start = s; 
     end = e; 
    } 

    #endregion 

    public enum PeriodType 
    { 
     SingleMonth, 
     AllMonths, 
     InterveningMonths 
    } 
    public bool Equals(FreeTime other) 
    { 

     ////Check whether the compared object is null. 
     //if (Object.ReferenceEquals(other, null)) return false; 

     //Check wether the products' properties are equal. 
     return start.Equals(other.start) && end.Equals(other.end); 
    } 


    public override int GetHashCode() 
    { 

     //Get hash code for the start field if it is not null. 
     int hashStart = start == null ? 0 : start.GetHashCode(); 

     //Get hash code for the end field. 
     int hashEnd = end.GetHashCode(); 

     //Calculate the hash code . 
     return hashStart^hashEnd; 
    } 

'

這是我如何使用containKey方法

Dictionary<FreeTime, string> FreeBusy = new Dictionary<FreeTime, string>();  
    if (FreeBusy.ContainsKey(intersection))  

,但我總是geeting True值

+2

你是否重載'Equals(object)'方法? – xanatos

+0

您可以顯示整個班級和一些示例數據嗎? –

+1

我推薦一下閱讀:[什麼是最好的算法重寫系統對象gethashcode](http://stackoverflow.com/questions/263400/what-is-the-最佳算法覆蓋系統對象gethashcode) –

回答

0

你還需要覆蓋object.Equals方法。只需添加功能

public override bool Equals(object obj) 
{ 
    if (obj is FreeTime) 
     return false; 
    return Equals((FreeTime)obj); 
} 

(你是剛剛實施IEquatable.Equals方法,而不是object.Equals方法,它有一個通用參數)

+0

感謝您的回覆,我將這個功能添加到我的代碼中,但仍然只是真的! – Ran

+0

奇怪的是,我做了一個小測試案例,它的工作 – user287107

0

這裏是背後的ContainsKey函數的代碼:

public bool ContainsKey(TKey key) { 
    return FindEntry(key) >= 0; 
} 
// Some code 
private int FindEntry(TKey key) { 
    if(key == null) { 
     ThrowHelper.ThrowArgumentNullException(ExceptionArgument.key); 
    } 

    if (buckets != null) { 
     int hashCode = comparer.GetHashCode(key) & 0x7FFFFFFF; 
     for (int i = buckets[hashCode % buckets.Length]; i >= 0; i = entries[i].next) { 
      if (entries[i].hashCode == hashCode && comparer.Equals(entries[i].key, key)) return i; 
     } 
    } 
    return -1; 
} 

這是不可能此代碼返回true:

Dictionary<FreeTime, string> FreeBusy = new Dictionary<FreeTime, string>();  
if (FreeBusy.ContainsKey(intersection)) 

由於字典是空的。


而這種代碼:

internal class FreeTime : IEquatable<FreeTime> 
{ 
    #region Constants 

    private const bool FirstDayOfWeekIsMonday = true; 

    #endregion 

    #region Private Variables 

    private static bool TodayIsSunday = (int) DateTime.Today.DayOfWeek == 0; 


    #endregion 

    #region Public Fields 

    public DateTime Start { get; set; } 

    public DateTime End { get; set; } 

    #endregion 

    #region Constructor 

    public FreeTime() 
    { 
     Start = End = DateTime.Today; 
    } 

    public FreeTime(DateTime s, DateTime e) 
    { 
     Start = s; 
     End = e; 
    } 

    #endregion 

    public enum PeriodType 
    { 
     SingleMonth, 
     AllMonths, 
     InterveningMonths 
    } 

    public bool Equals(FreeTime other) 
    { 

     ////Check whether the compared object is null. 
     //if (Object.ReferenceEquals(other, null)) return false; 

     //Check wether the products' properties are equal. 
     return Start.Equals(other.Start) && End.Equals(other.End); 
    } 


    public override int GetHashCode() 
    { 

     //Get hash code for the start field if it is not null. 
     int hashStart = Start.GetHashCode(); 

     //Get hash code for the end field. 
     int hashEnd = End.GetHashCode(); 

     //Calculate the hash code . 
     return hashStart^hashEnd; 
    } 
} 

行之有效我的電腦上。

+0

沒有字典不是空的,但我沒有顯示在這裏填寫字典的方法..謝謝大家的回覆,如果它與你合作,那麼它似乎我在其他地方遇到問題..我會盡力找出答案。再次感謝 – Ran

+0

@Ran提供更多代碼,我們將幫助您:) –