如何返回方法 C#

发布于 2024-12-13 15:56:09 字数 245 浏览 4 评论 0原文

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

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

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

谢谢

I need to return a method in an operator function.

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

I will need to do this for a multiply, subtract and divide operator/function too.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

怼怹恏 2024-12-20 15:56:09

您不能声明无参数运算符。您可以声明一个运算符来返回适当的委托 - 例如 Func - 但在我看来,这将是一件非常奇怪的事情。

如果您能告诉我们更多有关您想要实现的目标,我们可能会帮助您制定出更简洁的设计。

这是一个非常奇怪的重载一元 + 运算符的示例:

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);
    }
}

You can't declare parameterless operators. You can declare an operator to return an appropriate delegate - e.g. Func<int> - but it would be a pretty odd thing to do, IMO.

If you can tell us more about what you're trying to achieve, we can probably help you to work out a cleaner design.

Here's a pretty strange example overloading the unary + operator:

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));
    }
}

EDIT: If you're just trying to implement a Rational type, you're more likely to want:

public struct Rational
{
    // Other members

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

    public static Rational operator +(Rational left, Rational right)
    {
        return left.Add(right);
    }
}
‖放下 2024-12-20 15:56:09

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

This is what you SEEM to be trying to do, but your example makes it difficult to tell. So, from your comments in other answers it looks like you want to add, subtract, multiply, divide Rational numbers, which means the result should be a Rational as well (not an int).

Thus, you could define each of your methods, then implement operators to call those. The operators are always static, thus you'd need to check for null and handle as appropriate (in this case, I'll just throw 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);
    }
}
浮光之海 2024-12-20 15:56:09

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

return Add();

Simple. Just call the Add method:

return Add();
万劫不复 2024-12-20 15:56:09

我不认为你可以重载 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。

I don't think you can overload the + operator for int's! You would have to create your own wrapper class or struct instead:

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;
    }
}

Then you are free to do with '+' what ever you want to do with it.

The implicit operator automatically converts int's to MyInt. So you could assign like this: MyInt x = 7;

The explicit operator converts MyInt's to int's like: int i = (int)x; where x is a MyInt.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文