如何返回方法C#

Max*_*lmo 4 c# operators

我需要在运算符函数中返回一个方法.

        public int Add()
        {
            return 1;
        }
        public static int operator +()
        {
            return Add;
        }
Run Code Online (Sandbox Code Playgroud)

我还需要对乘法器,乘法和除法运算符/函数执行此操作.

谢谢

Jon*_*eet 23

您不能声明无参数运算符.你可以声明一个运算符来返回一个合适的委托 - 例如Func<int>- 但这是一件很奇怪的事情,IMO.

如果您可以告诉我们更多关于您想要实现的目标,我们可以帮助您制定更清洁的设计.

这是一个非常奇怪的例子,它重载了一元+运算符:

using System;

class Weird
{
    private readonly int amount;

    public Weird(int amount)
    {
        this.amount = amount;
    }

    private int Add(int original)
    {
        return original + amount;
    }

    // Very strange. Please don't do this.
    public static Func<int, int> operator +(Weird weird)
    {
        return weird.Add;
    }
}

class Test
{
    static void Main(string[] args)
    {
        Weird weird = new Weird(2);
        Func<int, int> func = +weird;
        Console.WriteLine(func(3));
    }
}
Run Code Online (Sandbox Code Playgroud)

编辑:如果你只是想尝试实现一个Rational类型,你更有可能想要:

public struct Rational
{
    // Other members

    public Rational Add(Rational other)
    {
        ...
    }

    public static Rational operator +(Rational left, Rational right)
    {
        return left.Add(right);
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 我的眼睛......护目镜什么都不做......; 0) (10认同)
  • @MaxWillmo:这个上下文不够......为什么运营商需要返回方法?它们将被赋予操作数,因此您应该将操作符的结果返回给操作数... (5认同)

Jam*_*are 9

这就是你要尝试做的事情,但是你的例子很难说清楚.所以,从你在其他答案中的评论来看,你看起来想要加,减,乘,除除有理数,这意味着结果也应该是一个Rational(不是一个int).

因此,您可以定义每个方法,然后实现运算符来调用它们.运算符总是静态的,因此您需要检查null并处理(在这种情况下,我只是抛出ArgumentNullException):

public class Rational
{
    public Rational Add(Rational other)
    {
        if (other == null) throw new ArgumentNullException("other");

        return // <-- return actual addition result here
    }

    public static Rational operator +(Rational left, Rational right)
    {
        if (left == null) throw new ArgumentNullException("left");

        return left.Add(right);
    }

    public Rational Subtract(Rational other)
    {
        if (other == null) throw new ArgumentNullException("other");

        return  // <-- return actual subtraction result here
    }

    public static Rational operator -(Rational left, Rational right)
    {
        if (left == null) throw new ArgumentNullException("left");

        return left.Subtract(right);
    }

    public Rational Multiply(Rational other)
    {
        if (other == null) throw new ArgumentNullException("other");

        return // <-- return actual multiplication result here
    }

    public static Rational operator *(Rational left, Rational right)
    {
        if (left == null) throw new ArgumentNullException("left");

        return left.Multiply(right);
    }

    public Rational Divide(Rational other)
    {
        if (other == null) throw new ArgumentNullException("other");

        return  // <-- return actual division result here
    }

    public static Rational operator /(Rational left, Rational right)
    {
        if (left == null) throw new ArgumentNullException("left");

        return left.Divide(right);
    }
}
Run Code Online (Sandbox Code Playgroud)