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;
}

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
已經足夠痛苦的比較。
- 2 回答
- 0 關注
- 147 瀏覽
添加回答
舉報