Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to return a Method C#

Tags:

operators

c#

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

like image 569
MaxWillmott Avatar asked Nov 28 '22 02:11

MaxWillmott


2 Answers

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);
    }
}
like image 173
Jon Skeet Avatar answered Nov 29 '22 17:11

Jon Skeet


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);
    }
}
like image 27
James Michael Hare Avatar answered Nov 29 '22 17:11

James Michael Hare