Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/258.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 实施操作员覆盖/均衡<;T>;一次针对所有原始数字数据类型?_C#_Operator Overloading - Fatal编程技术网

C# 实施操作员覆盖/均衡<;T>;一次针对所有原始数字数据类型?

C# 实施操作员覆盖/均衡<;T>;一次针对所有原始数字数据类型?,c#,operator-overloading,C#,Operator Overloading,我想实现我自己的CustomNumber类,并使用关系运算符将其与所有其他原始数字数据类型(int、long、double、float等)进行比较 是否有一种方法可以一次为所有这些对象执行此操作,或者我真的必须覆盖==,!=,>,=本质上没有捷径,你必须为你想要支持的所有类型实现所有的操作符和功能,它无法读懂你的心思 查看十进制 出于理智考虑,您会注意到并非所有内容都需要重写,这是通过隐式运算符的实现来实现的: public static implicit operator Decimal(by

我想实现我自己的CustomNumber类,并使用关系运算符将其与所有其他原始数字数据类型(int、long、double、float等)进行比较

是否有一种方法可以一次为所有这些对象执行此操作,或者我真的必须覆盖==,!=,>,=本质上没有捷径,你必须为你想要支持的所有类型实现所有的操作符和功能,它无法读懂你的心思

查看
十进制

出于理智考虑,您会注意到并非所有内容都需要重写,这是通过隐式运算符的实现来实现的:

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;
}
公共静态隐式运算符十进制(字节值)
{
返回新的小数(值);
}
[CLSCompliant(false)]
公共静态隐式运算符十进制(sbyte值)
{
返回新的小数(值);
}
公共静态隐式运算符十进制(短值)
{
返回新的小数(值);
}
[CLSCompliant(false)]
公共静态隐式运算符十进制(ushort值)
{
返回新的小数(值);
}
公共静态隐式运算符十进制(字符值)
{
返回新的小数(值);
}
公共静态隐式运算符十进制(int值)
{
返回新的小数(值);
}
[CLSCompliant(false)]
公共静态隐式运算符十进制(uint值)
{
返回新的小数(值);
}
公共静态隐式运算符十进制(长值)
{
返回新的小数(值);
}
[CLSCompliant(false)]
公共静态隐式运算符十进制(ulong值)
{
返回新的小数(值);
}
公共静态显式运算符十进制(浮点值)
{
返回新的小数(值);
}
公共静态显式运算符十进制(双精度值)
{
返回新的小数(值);
}
公共静态显式运算符字节(十进制值)
{
返回ToByte(值);
}
[CLSCompliant(false)]
公共静态显式运算符sbyte(十进制值)
{
返回ToSByte(值);
}
公共静态显式运算符字符(十进制值)
{
UInt16温度;
尝试
{
温度=ToUInt16(值);
}
捕获(溢出例外e)
{
抛出新的OverflowException(Environment.GetResourceString(“Overflow_Char”),e);
}
返回(字符)温度;
}
公共静态显式运算符short(十进制值)
{
返回到16(值);
}
[CLSCompliant(false)]
公共静态显式运算符ushort(十进制值)
{
返回ToUInt16(值);
}
公共静态显式运算符int(十进制值)
{
返回到32(值);
}
[CLSCompliant(false)]
公共静态显式运算符uint(十进制值)
{
返回ToUInt32(值);
}
公共静态显式运算符long(十进制值)
{
返回到64(值);
}
[CLSCompliant(false)]
公共静态显式运算符ulong(十进制值)
{
返回ToUInt64(值);
}
公共静态显式运算符浮点(十进制值)
{
返回到单个(值);
}
公共静态显式运算符双精度(十进制值)
{
返回到double(值);
}
公共静态十进制运算符+(十进制d)
{
返回d;
}
公共静态十进制运算符-(十进制d)
{
返回否定(d);
}
公共静态十进制运算符++(十进制d)
{
返回Add(d,1);
}
公共静态十进制运算符--(十进制d)
{
返回减法(d,1);
}
[System.Security.SecuritySafeCritical]//自动生成
公共静态十进制运算符+(十进制d1,十进制d2)
{
FCallAddSub(参考d1、参考d2、十进制添加);
返回d1;
}
[System.Security.SecuritySafeCritical]//自动生成
公共静态十进制运算符-(十进制d1,十进制d2)
{
FCallAddSub(参考d1、参考d2、十进制值);
返回d1;
}
[System.Security.SecuritySafeCritical]//自动生成
公共静态十进制运算符*(十进制d1,十进制d2)
{
FCallMultiply(参考d1,参考d2);
返回d1;
}
[System.Security.SecuritySafeCritical]//自动生成
公共静态十进制运算符/(十进制d1,十进制d2)
{
FCallDivide(参考d1、参考d2);
返回d1;
}
公共静态十进制运算符%(十进制d1,十进制d2)
{
返回剩余部分(d1、d2);
}
[System.Security.SecuritySafeCritical]//自动生成
公共静态布尔运算符==(十进制d1,十进制d2)
{
返回FCallCompare(参考d1,参考d2)==0;
}
[System.Security.SecuritySafeCritical]//自动生成
公共静态布尔运算符=(小数点d1,小数点d2)
{
返回FCallCompare(参考d1,参考d2)!=0;
}
[System.Security.SecuritySafeCritical]//自动生成
公共静态布尔运算符=(十进制d1,十进制d2)
{
返回FCallCompare(参考d1,参考d2)>=0;
}
本质上没有捷径,你必须为你想要支持的所有类型实现所有的操作符和功能,它无法读懂你的心思

查看
十进制

出于理智考虑,您会注意到并非所有内容都需要重写,这是通过隐式运算符的实现来实现的:

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;
}
公共静态隐式运算符十进制(字节值)
{
返回新的小数(值);
}
[CLSCompliant(false)]
公共静态隐式运算符十进制(sbyte值)
{
返回新的小数(值);
}
公共静态隐式运算符十进制(短值)
{
返回新的小数(值);
}
[CLSCompliant(false)]
公共静态隐式运算符十进制(ushort值)
{
返回新的小数(值);
}
公共静态隐式运算符十进制(字符值)
{
返回新的小数(值);
}
公共静态隐式运算符十进制(int值)
{
返回新的小数(值);
}
[CLSCompliant(false)]
公共静态隐式运算符十进制(uint值)
{
返回新的小数(值);
}
公共静态隐式运算符十进制(长值)
{
返回新的小数(值);
}
[CLSCompliant(false)]
公共静态隐式运算符十进制(ulong值)
{
返回新的小数(值);
}
公共静态显式运算符十进制(浮点值)
{
返回新的小数(值);
}
公共静态显式运算符十进制(双精度值)
{
返回新的小数(值);
}
公共静态显式运算符字节(十进制值)
{
返回ToByte(值);
}
[CLSCompliant(false)]
公共图书馆