What should GetHashCode return when object's identifier is null?

前端 未结 3 680
夕颜
夕颜 2021-02-05 04:55

Which of the following is correct/better, considering that identity property could be null.

public override int GetHashCode()
{
    if (ID == null) {
        ret         


        
3条回答
  •  悲哀的现实
    2021-02-05 05:44

    Perhaps what you want is something like this?

    override int GetHashCode()
    {
        if (ID != null)
            return ID.GetHashCode();
    
        return DBNull.Value.GetHashCode();
    }
    

    The important thing is this, should two objects with null IDs be considered equal?

提交回复
热议问题