How to return a C # method

I need to return the method to an operator function.

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

I will need to do this to multiply, subtract and divide the operator / function.

thanks

+4
source share
5 answers

You cannot declare without parameters. You can declare the statement by returning the appropriate delegate - for example, Func<int> - but that would be pretty strange IMO.

If you can tell us more about what you are trying to achieve, we can probably help you develop a cleaner design.

Here's a rather strange example of 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 are just trying to implement a Rational type, you most likely want:

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

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

That way, you can define each of your methods, and then implement statements to call them. Operators are always static, so you will need to check for null and handle accordingly (in this case, I just throw an 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); } } 
+9
source

Simple Just call the Add method:

 return Add(); 
+3
source

I do not think you can overload the + operator for int! Instead, you have to create your own wrapper class or structure:

 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 can freely do with "+", what would you do with it.

An implicit statement automatically converts an int to MyInt. Thus, you can assign the following: MyInt x = 7;

The explicit operator converts MyInt to int as: int i = (int)x; where x is MyInt.

0
source

Source: https://habr.com/ru/post/1379516/


All Articles