# 使用C#,如何一次重载所有运算符?

Using c#, how can I overload all operators at once?
2021-03-07
•  译文(汉语)
•  原文(英语)

``````public struct Money
{

private Money(decimal qty)
{
_quantity = Math.Round(qty, 2);
}

public static implicit operator Money(decimal dec)
{
return new Money(dec);
}
}
``````

``````public static implicit operator decimal(Money money)
{
return money._quantity;
}
``````

``````public struct Money: IComparble<Money> {

...

// C# doesn't have <=> operator, alas...
public static int Compare(Money left, Money right) {
if (left._quantity < right._quantity)
return -1;
else if (left._quantity > right._quantity)
return 1;
else
return 0;
}

public static Boolean operator == (Money left, Money right) {
return Compare(left, right) == 0;
}

public static Boolean operator != (Money left, Money right) {
return Compare(left, right) != 0;
}

public static Boolean operator > (Money left, Money right) {
return Compare(left, right) > 0;
}

public static Boolean operator < (Money left, Money right) {
return Compare(left, right) < 0;
}

public static Boolean operator >= (Money left, Money right) {
return Compare(left, right) >= 0;
}

public static Boolean operator <= (Money left, Money right) {
return Compare(left, right) <= 0;
}

public int CompareTo(Money other) {
return Compare(this, other);
}
}
``````

:是的,有时缺少"<=>"运算符(可以将所有比较组合在一起)和数值类型(用于实现算术)的麻烦.

I have a struct like this.

``````public struct Money
{

private Money(decimal qty)
{
_quantity = Math.Round(qty, 2);
}

public static implicit operator Money(decimal dec)
{
return new Money(dec);
}
}
``````

For `Money` do I have to overload all operators +, -, <, <=, >, >=, ==, !=, etc.? Or is there a way to accept all operators of `decimal` for `Money` as well? As you see `Money` has only one field which is `_quantity`. I want that all operators asked for `Money` should return as if it is asked for _quantity.

``````public static implicit operator decimal(Money money)
{
return money._quantity;
}
``````

I am creating `Money` struct, because I do not want to use `decimal` in my entire project. Compiler should force me to use `Money` instead of `decimal`. If I use above conversion operator implicitly, it wil contradict the reason behind creating this struct. Thanks in advance...

Solutions1

You have to implement all the operators separately, but you can simplify the process by inmplementing `static Compare` method (in order to emulate `<=>` operator which is not supported by C#):

``````public struct Money: IComparble<Money> {

...

// C# doesn't have <=> operator, alas...
public static int Compare(Money left, Money right) {
if (left._quantity < right._quantity)
return -1;
else if (left._quantity > right._quantity)
return 1;
else
return 0;
}

public static Boolean operator == (Money left, Money right) {
return Compare(left, right) == 0;
}

public static Boolean operator != (Money left, Money right) {
return Compare(left, right) != 0;
}

public static Boolean operator > (Money left, Money right) {
return Compare(left, right) > 0;
}

public static Boolean operator < (Money left, Money right) {
return Compare(left, right) < 0;
}

public static Boolean operator >= (Money left, Money right) {
return Compare(left, right) >= 0;
}

public static Boolean operator <= (Money left, Money right) {
return Compare(left, right) <= 0;
}

public int CompareTo(Money other) {
return Compare(this, other);
}
}
``````
Talk1:
Thank you for your answer Dmitry. But my operator overload methods already have the structure `return left._quantity<operator>right._quantity`. If c# have supported generic operators as well as generic types, then my question would have an answer.
Talk2:
: Yes, the absence of "<=>" operator (which can combine all the comparisons) and Numeric type (for implementing arithmetics) is sometimes annoying.