如何返回一个方法c#

本文关键字:一个 方法 返回 何返回 | 更新日期: 2023-09-27 18:13:31

我需要在操作符函数中返回一个方法。

        public int Add()
        {
            return 1;
        }
        public static int operator +()
        {
            return Add;
        }

对于乘法、减法和除法运算符/函数也需要这样做。

谢谢

如何返回一个方法c#

不能声明无参数操作符。你可以声明一个操作符来返回一个适当的委托-例如Func<int> -但这将是一个非常奇怪的事情,我认为。

如果你能告诉我们更多关于你想要达到的目标,我们也许可以帮助你制定一个更干净的设计。

下面是一个非常奇怪的重载一元+操作符的例子:
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));
    }
}

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

public struct Rational
{
    // Other members
    public Rational Add(Rational other)
    {
        ...
    }
    public static Rational operator +(Rational left, Rational right)
    {
        return left.Add(right);
    }
}

这就是你似乎想要做的,但你的例子使它很难告诉。因此,从您在其他答案中的评论来看,您似乎想要加、减、乘、除有理数,这意味着结果也应该是一个有理数(而不是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);
    }
}

很简单。只需调用Add方法:

return Add();

C Sharp -第18课:重载操作符

我不认为你可以重载整型的+操作符!您必须创建自己的包装器类或结构:

public struct MyInt
{
    private int _value;
    public MyInt(int value)
    {
        _value = value;
    }
    public int Value
    {
        get { return _value; }
    }
    public static MyInt operator +(MyInt a, MyInt b)
    {
        return new MyInt(a._value + b._value);
    }
    public static implicit operator MyInt(int intValue)
    {
        return new MyInt(intValue);
    }
    public static explicit operator int(MyInt x)
    {
        return x.Value;
    }
}

那么你就可以自由地用"+"做任何你想做的事情。

隐式操作符自动将int型转换为MyInt型。所以你可以这样赋值:MyInt x = 7;

显式操作符将MyInt型转换为int型,如:int i = (int)x;,其中x是MyInt型。