【问题标题】:How to return a Method C#如何返回方法 C#
【发布时间】:2011-11-03 16:52:54
【问题描述】:

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

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

我也需要为乘法、减法和除法运算符/函数执行此操作。

谢谢

【问题讨论】:

  • 你是说需要返回方法调用的结果吗?还是返回一个方法委托?
  • 我需要返回Add()的返回值
  • 停止投票!这是一个属于 StackOverflow 的合法编程问题,无论实际意图多么荒谬。一些最佳问题涉及语言中晦涩或奇怪的事物。
  • @DavidPfeffer:我认为 OP 实际上不应该在这里做任何奇怪的事情 - 我怀疑他只是误解或误解了他 的意图。
  • 在我看来他希望+ 运算符使用Add 函数(几乎就像运算符本身是一个计算为函数的表达式,然后操作数成为参数)。几乎就像编译时(或者可能是运行时)替换。

标签: c# operators


【解决方案1】:

您不能声明无参数运算符。您可以声明一个运算符以返回一个适当的委托 - 例如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));
    }
}

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

public struct Rational
{
    // Other members

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

    public static Rational operator +(Rational left, Rational right)
    {
        return left.Add(right);
    }
}

【讨论】:

  • 我的眼睛...护目镜什么也没做... ;0)
  • “实现方法加、减、乘、除以及运算符”这些用于有理数项目。
  • @MaxWillmo:这还不够上下文......为什么操作员需要返回方法?它们将被赋予操作数,因此您应该返回将运算符应用于操作数的结果...
【解决方案2】:

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

【讨论】:

    【解决方案3】:

    简单。只需调用 Add 方法:

    return Add();
    

    【讨论】:

    • 错别字,那也不行。
    • 他需要对派生对象执行+运算符。由于 Add 函数重载原始运算符结果将始终为 1。
    【解决方案4】:
    【解决方案5】:

    我认为您不能为 int 重载 + 运算符!您必须创建自己的包装类或结构:

    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。

    【讨论】:

    • 也可以重载++运算符:public static MyInt operator ++(MyInt a) { return new MyInt(a._value + 1); }
    猜你喜欢
    • 2023-03-18
    • 2013-07-04
    • 1970-01-01
    • 2016-01-21
    • 2012-04-18
    • 1970-01-01
    • 2020-07-14
    • 2016-09-12
    相关资源
    最近更新 更多