What I'm trying to do, is to call a specific function for a specific Key. For example, if the is key is ' ', I want to call Sum function. I already created a Dictionary, and added a Key and a function.
Func<int, int, int> Sum = (a, b) => a b;
Dictionary<char, Func<int, int, int>> operations = new Dictionary<char, Func<int, int, int>>()
operations.Add(' ', Sum);
I don't understand how to pass a values to my Sum Func, and how to store an answer somewhere.
CodePudding user response:
I don't understand how to pass a values to my Sum Func, and how to store an answer somewhere.
var operation = operations[' '];
var result = operation(1, 2); // yields 3
(fiddle)
If you want to be overly verbose, you can write operation.Invoke(1, 2)
instead of operation(1, 2)
.