Store an operator in a variable

前端 未结 5 966
情深已故
情深已故 2020-12-17 16:00

Is there a way to store an operator inside a variable? I want to do something like this (pseudo code):

void MyLoop(int start, int finish, operator op)
{
             


        
相关标签:
5条回答
  • 2020-12-17 16:23

    use something like Func<int, int> op

    or change the type of op to string, then check the value and according to it build your for loop like:

    void MyLoop(int start, int finish, string op)
    {
        if ((op.Equals("++") && (start < finish))
        {
          for(var i = start; i < finish; i++)
          {
              //processMethod(i)
          }
        }
        else if ((op.Equals("--") && (start > finish))
        {
          for(var i = start; i < finish; i--)
          {
              //processMethod(i)
          }
        }
    }
    
    0 讨论(0)
  • 2020-12-17 16:28

    I suppose something like this. You do not define the operator, but a function (lambda) which does the change for you.

    void MyLoop(int start, int finish, Func<int, int> op)
    {
        for(var i = start; i < finish; i = op(i))
        {
            //do stuff with i
        }
    }
    

    I could then call this method like so:

    MyLoop(15, 45, x => x+1);
    MyLoop(60, 10, x => x-1);
    
    0 讨论(0)
  • 2020-12-17 16:34

    Use a Function delegate;

    Encapsulates a method that has one parameter and returns a value of the type specified by the TResult parameter.

    void MyLoop(int start, int finish, Func<int, int> op)
    {
        for(var i = start; i < finish; i = op(i))
        {
            //do stuff with i
        }
    }
    

    Then;

    MyLoop(15, 45, x => ++x);
    MyLoop(60, 10, x => --x);
    

    Here is a DEMO.

    0 讨论(0)
  • 2020-12-17 16:34
    public class Program {
        public static void Main(String[] args) {
            Looper(x => x + 1);
            Looper(x => ++x);
            //Looper(x => x++); will not works
            Looper(x => x * 2);
        }
    
        public static void Looper(Func<int, int> op) {
            for (int i = 1; i < 10; i = op(i)) {
                Console.WriteLine(i);
            }
            Console.WriteLine("----------");
        }
    
    } 
    
    0 讨论(0)
  • 2020-12-17 16:40

    I tried a different approach, using a class that defines operators and accessing via reflection - i.e. you can store your operators as strings. This allows for relational operators as well.

    class Program
    {
        static void Main(string[] args)
        {
            Operators ops = new Operators();
            object result = ops.Use("LessOrEqual", new object[] {3,2}); // output: False
            Console.WriteLine(result.ToString());
    
            result =  ops.Use("Increment", new object[] {3}); // output: 4
            Console.WriteLine(result.ToString());
            Console.ReadKey();
        }
    }
    
    public class Operators
    {
        public object Use(String methodName, Object[] parameters)
        {
            object result;
            MethodInfo mInfo = this.GetType().GetMethod(methodName);
            result = mInfo.Invoke(this, parameters); // params for operator, komma-divided
            return result;
        }
    
    
        public bool LessOrEqual(int a, int b)
        {
            if (a <= b)
            {
                return true;
            }
            else
            {
                return false; 
            }  
        }
    
        public int Increment(int a)
        {
            return ++a;
        }
    }
    
    0 讨论(0)
提交回复
热议问题