【问题】
之前已经折腾成功了,C#中,将函数当做参数传递过去,不过用的是delegate:
参考某人评论,现在去实现对应的action或者是Func的版本。
【解决过程】
1.参考:
C# Passing Function as Argument
经过尝试,使用如下代码实现,Action<paras>的形式
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | //declaration and implementation of function public void updateProgress( int percentage) { pgbDownload.Value = percentage; } //call some other func, pass the function as parameter getUrlRespStreamBytes(xxx, updateProgress); //some other related function public int getUrlRespStreamBytes(xxx, Action< int > funcUpdateProgress) { //... if (funcUpdateProgress != null ) { int currentPercent = 20; funcUpdateProgress(currentPercent); } //... } |
【总结】
- 此处是当做参数要传递的函数没有返回值,所以可以用Action<paras>的形式;
- 如果当做参数要传递的函数,是有返回值的话,就要改为Func<paras, returnValue>的形式了;