如何使用字典中的 Func
我想做的是为特定的键调用特定的函数。 例如,如果 is 键是“+”,我想调用 Sum 函数。 我已经创建了一个字典,并添加了一个键和一个函数。
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);
我不明白如何将值传递给我的 Sum Func,以及如何将答案存储在某处。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
(fiddle)
如果你想强调
操作
是委托而不是“常规”方法,您可以编写operation.Invoke(1, 2)
而不是operation(1, 2)
。(fiddle)
If you want to emphasize that
operation
is a delegate rather than a "regular" method, you can writeoperation.Invoke(1, 2)
instead ofoperation(1, 2)
.