第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何覆蓋!= 運算符?

如何覆蓋!= 運算符?

C#
BIG陽 2022-01-16 15:07:36
我嘗試覆蓋 != 運算符:public class Box{    public Box()    {    }    public Box(double height, double width)    {        Height = height;        Width = width;    }    public double Height { get; set; }    public double Width { get; set; }    public override int GetHashCode()    {        unchecked        {            return (Height.GetHashCode() * 397) ^ Width.GetHashCode();        }    }    public override bool Equals(object obj)    {        if (ReferenceEquals(null, obj)) return false;        if (ReferenceEquals(this, obj)) return true;        return obj.GetType() == GetType() && Equals((Box)obj);    }    protected bool Equals(Box other)    {        return Math.Abs(Height - other.Height) + Math.Abs(Width - other.Width) < 0.001;    }    public static bool operator ==(Box left, Box right)    {        if (ReferenceEquals(null, left))            return false;        if (ReferenceEquals(null, right))            return false;        return left.Equals(right);    }    public static bool operator !=(Box left, Box right)    {        var t = !(left == right);        return t;    }}public class BetterBox:Box{}并嘗試使用 != 運算符var box = new Box();var betterBox = box as BetterBox;if(betterBox!=null){    --do-something}在這種情況下 != returntrue并且代碼進入if. 這里有什么問題?為什么會發(fā)生?在 mdsn 我看到相同的代碼:https ://docs.microsoft.com/en-us/previous-versions/dotnet/netframework-4.0/336aedhh%28v%3dvs.100%29
查看完整描述

2 回答

?
慕姐8265434

TA貢獻1813條經(jīng)驗 獲得超2個贊

這是Box使用相等檢查的完整實現(xiàn)(實現(xiàn)IEquatable<Box>)。以下是測試結果:


       a        b     a==b     a!=b    a.Equals(b)    b.Equals(a)

    null     null     True    False                              

 [30×10]     null    False     True          False               

 [30×10]  [30×10]     True    False           True           True

    null  [30×10]    False     True                         False

我已經(jīng)實現(xiàn)了Equals(Box), Equals(object), GetHashCode(), operator ==,operator !=和ToString().


public class Box : IEquatable<Box>

{

    // Place values in constants

    public const double SizeTolerance = 0.001;


    public double Width { get; set; }

    public double Height { get; set; }


    public static bool operator ==(Box left, Box right)

    {

        if(!ReferenceEquals(left, null))

        {

            // consider that left.Equals(null) should return false

            return left.Equals(right);

        }

        return ReferenceEquals(left, right);

    }

    public static bool operator !=(Box left, Box right)

    {

        return !(left==right);

    }


    #region IEquatable Members

    /// <summary>

    /// Equality overrides from <see cref="System.Object"/>

    /// </summary>

    /// <param name="obj">The object to compare this with</param>

    /// <returns>False if object is a different type, otherwise it calls <code>Equals(Box)</code></returns>

    public override bool Equals(object obj)

    {

        if(obj is Box other)

        {

            return Equals(other);

        }

        return false;

    }


    /// <summary>

    /// Checks for equality among <see cref="Box"/> classes

    /// </summary>

    /// <param name="other">The other <see cref="Box"/> to compare it to</param>

    /// <returns>True if equal</returns>

    public bool Equals(Box other)

    {

        if(ReferenceEquals(other, null))

        {

            return false;

        }

        return Math.Abs(Width-other.Width)<SizeTolerance

            && Math.Abs(Height-other.Height)<SizeTolerance;

    }


    /// <summary>

    /// Calculates the hash code for the <see cref="Box"/>

    /// </summary>

    /// <returns>The int hash value</returns>

    public override int GetHashCode()

    {

        unchecked

        {

            int hc = 17;

            hc = 23*hc + Width.GetHashCode();

            hc = 23*hc + Height.GetHashCode();

            return hc;

        }

    }


    #endregion


    public override string ToString()

    {

        return $"[{Width}×{Height}]";

    }

}

以及測試它的代碼:


    static void Main(string[] args)

    {

        Debug.WriteLine($"{"a",8} {"b",8} {"a==b",8} {"a!=b",8} {"a.Equals(b)",14} {"b.Equals(a)",14}");

        Box a = null;

        Box b = null;

        Debug.WriteLine($"{a?.ToString()??"null",8} {b?.ToString()??"null",8} {a==b,8} {a!=b,8} {a?.Equals(b),14} {b?.Equals(a),14}");

        a = new Box() { Height = 10, Width = 30 };

        Debug.WriteLine($"{a?.ToString()??"null",8} {b?.ToString()??"null",8} {a==b,8} {a!=b,8} {a?.Equals(b),14} {b?.Equals(a),14}");

        b = new Box() { Height = 10, Width = 30 };

        Debug.WriteLine($"{a?.ToString()??"null",8} {b?.ToString()??"null",8} {a==b,8} {a!=b,8} {a?.Equals(b),14} {b?.Equals(a),14}");

        a = null;

        Debug.WriteLine($"{a?.ToString()??"null",8} {b?.ToString()??"null",8} {a==b,8} {a!=b,8} {a?.Equals(b),14} {b?.Equals(a),14}");

    }

}


查看完整回答
反對 回復 2022-01-16
?
千巷貓影

TA貢獻1829條經(jīng)驗 獲得超7個贊

您對==運算符的實現(xiàn)是錯誤的。檢查空值時需要考慮兩個操作數(shù)。目前,如果left為 null,則返回 false,忽略right操作數(shù)的值。如果他們兩個都是null它應該返回true。


public static bool operator ==(Box left, Box right)

{

    var isLeftNull = ReferenceEquals(null, left);

    var isRightNull = ReferenceEquals(null, right);


    if (isLeftNull && isRightNull)

    {

        return true;

    }


    if (isLeftNull || isRightNull)

    {

        return false;

    }


    return left.Equals(right);

}


查看完整回答
反對 回復 2022-01-16
  • 2 回答
  • 0 關注
  • 149 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網(wǎng)微信公眾號