亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

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

立即為所有原始數字數據類型實現運算符覆蓋/Equatable<T>?

立即為所有原始數字數據類型實現運算符覆蓋/Equatable<T>?

C#
拉丁的傳說 2023-09-16 17:49:20
我想實現我自己的 CustomNumber 類,并使用關系運算符將其與所有其他原始數字數據類型(int、long、double、float 等)進行比較。有沒有辦法同時對所有這些操作符執行此操作,或者我真的必須重寫 ==、!=、>、<、>= 和 <= 運算符以及每個單個操作符的 Equals(T other) 方法整數數據類型?我想我知道運算符重載一般是如何工作的,但感覺必須有某種快捷方式才能使我的 CustomNumber 與使用關系運算符的所有原始數字數據類型進行比較,而不是必須為每個運算符重載這 6 個運算符中的每一個單一數據類型,可能很快就會添加大約 100 個重載定義。
查看完整描述

2 回答

?
慕森卡

TA貢獻1806條經驗 獲得超8個贊

本身沒有捷徑,您必須為您想要支持的所有類型實現所有運算符和功能,它無法讀懂您的想法。

檢查decimal?實施情況。

為了您的理智,您會注意到并非所有內容都需要被覆蓋,這是通過隱式運算符的實現來完成的:

public static implicit operator Decimal(byte value)

{

? ?return new Decimal(value);

}


[CLSCompliant(false)]

public static implicit operator Decimal(sbyte value)

{

? ?return new Decimal(value);

}


public static implicit operator Decimal(short value)

{

? ?return new Decimal(value);

}


[CLSCompliant(false)]

public static implicit operator Decimal(ushort value)

{

? ?return new Decimal(value);

}


public static implicit operator Decimal(char value)

{

? ?return new Decimal(value);

}


public static implicit operator Decimal(int value)

{

? ?return new Decimal(value);

}


[CLSCompliant(false)]

public static implicit operator Decimal(uint value)

{

? ?return new Decimal(value);

}


public static implicit operator Decimal(long value)

{

? ?return new Decimal(value);

}


[CLSCompliant(false)]

public static implicit operator Decimal(ulong value)

{

? ?return new Decimal(value);

}



public static explicit operator Decimal(float value)

{

? ?return new Decimal(value);

}


public static explicit operator Decimal(double value)

{

? ?return new Decimal(value);

}


public static explicit operator byte(Decimal value)

{

? ?return ToByte(value);

}


[CLSCompliant(false)]

public static explicit operator sbyte(Decimal value)

{

? ?return ToSByte(value);

}


public static explicit operator char(Decimal value)

{

? ?UInt16 temp;

? ?try

? ?{

? ? ? temp = ToUInt16(value);

? ?}

? ?catch (OverflowException e)

? ?{

? ? ? throw new OverflowException(Environment.GetResourceString("Overflow_Char"), e);

? ?}

? ?return (char)temp;

}


public static explicit operator short(Decimal value)

{

? ?return ToInt16(value);

}


[CLSCompliant(false)]

public static explicit operator ushort(Decimal value)

{

? ?return ToUInt16(value);

}


public static explicit operator int(Decimal value)

{

? ?return ToInt32(value);

}


[CLSCompliant(false)]

public static explicit operator uint(Decimal value)

{

? ?return ToUInt32(value);

}


public static explicit operator long(Decimal value)

{

? ?return ToInt64(value);

}


[CLSCompliant(false)]

public static explicit operator ulong(Decimal value)

{

? ?return ToUInt64(value);

}


public static explicit operator float(Decimal value)

{

? ?return ToSingle(value);

}


public static explicit operator double(Decimal value)

{

? ?return ToDouble(value);

}


public static Decimal operator +(Decimal d)

{

? ?return d;

}


public static Decimal operator -(Decimal d)

{

? ?return Negate(d);

}


public static Decimal operator ++(Decimal d)

{

? ?return Add(d, One);

}


public static Decimal operator --(Decimal d)

{

? ?return Subtract(d, One);

}


[System.Security.SecuritySafeCritical]? // auto-generated

public static Decimal operator +(Decimal d1, Decimal d2)

{

? ?FCallAddSub(ref d1, ref d2, DECIMAL_ADD);

? ?return d1;

}


[System.Security.SecuritySafeCritical]? // auto-generated

public static Decimal operator -(Decimal d1, Decimal d2)

{

? ?FCallAddSub(ref d1, ref d2, DECIMAL_NEG);

? ?return d1;

}


[System.Security.SecuritySafeCritical]? // auto-generated

public static Decimal operator *(Decimal d1, Decimal d2)

{

? ?FCallMultiply(ref d1, ref d2);

? ?return d1;

}


[System.Security.SecuritySafeCritical]? // auto-generated

public static Decimal operator /(Decimal d1, Decimal d2)

{

? ?FCallDivide(ref d1, ref d2);

? ?return d1;

}


public static Decimal operator %(Decimal d1, Decimal d2)

{

? ?return Remainder(d1, d2);

}


[System.Security.SecuritySafeCritical]? // auto-generated

public static bool operator ==(Decimal d1, Decimal d2)

{

? ?return FCallCompare(ref d1, ref d2) == 0;

}


[System.Security.SecuritySafeCritical]? // auto-generated

public static bool operator !=(Decimal d1, Decimal d2)

{

? ?return FCallCompare(ref d1, ref d2) != 0;

}


[System.Security.SecuritySafeCritical]? // auto-generated

public static bool operator <(Decimal d1, Decimal d2)

{

? ?return FCallCompare(ref d1, ref d2) < 0;

}


[System.Security.SecuritySafeCritical]? // auto-generated

public static bool operator <=(Decimal d1, Decimal d2)

{

? ?return FCallCompare(ref d1, ref d2) <= 0;

}


[System.Security.SecuritySafeCritical]? // auto-generated

public static bool operator >(Decimal d1, Decimal d2)

{

? ?return FCallCompare(ref d1, ref d2) > 0;

}


[System.Security.SecuritySafeCritical]? // auto-generated

public static bool operator >=(Decimal d1, Decimal d2)

{

? ?return FCallCompare(ref d1, ref d2) >= 0;

}


查看完整回答
反對 回復 2023-09-16
?
慕少森

TA貢獻2019條經驗 獲得超9個贊

作為部分快捷方式,您可以限制double覆蓋所有常規“數字”類型,但需要編譯器插入強制轉換。double對于較大的整數值,總是比較是危險的(int并且較小的類型始終是精確的)。


class MyType?

{

? ? public override bool Equals(object obj)

? ? {

? ? ? ? return base.Equals(obj);

? ? }

? ? public override int GetHashCode()

? ? {

? ? ? ? return base.GetHashCode();

? ? }

? ? public static bool operator == (MyType x, double c)

? ? {

? ? ? ?// write some real code here - this one does not have a value to compare to.?

? ? ? ?return c > 42;?

? ? }


? ? // you need all several overrides for each operator to behave in expected way

? ? // so calling the same one (a == b)

? ? // from a != b, b != a, b == a is a way to keep them consistent

? ? public static bool operator == (double c, MyType x)

? ? {

? ? ? ? return (x == c);

? ? }


? ? public static bool operator != (double c, MyType x)

? ? {

? ? ? ? return !(c == x);? ?

? ? }


? ? public static bool operator != (MyType x, double c)

? ? {

? ? ? ? return !(x == c);

? ? }

}

筆記

  • C#/.NET 沒有內置的“數字”類型概念 - 這是人們經常問的問題(即,是否存在將我的泛型方法限制為數字類型的約束?,泛型 - 其中 T 是數字?)。

  • 不要忘記實施IEquatable<T>,?IComparable,?IComparable<T>...

  • 考慮一下它是否真的對你的“數字”有用,能夠自由地與其他類型混合 - 特別是與不精確float/double已經足夠痛苦的比較。


查看完整回答
反對 回復 2023-09-16
  • 2 回答
  • 0 關注
  • 147 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號