What is Action in C#?
来源:互联网 发布:梁朝伟愿你知我心 编辑:程序博客网 时间:2024/06/06 07:19
转的,没有中文版,别怪我啊....
One of my friends called me after his interview for a developer role in an esteemed organization. One of the questions the interviewer asked him was:
After talking to him, I thought why not blog about it? I am trying here to use a minimum of words and optimum code samples to discuss the answer of this interview question.
An Action is a type of delegate:
- It returns no value.
- It may take 0 parameter to 16 parameters.
For example the following Action can encapsulate a method taking two integers as input parameters and returning void.
So if you have a method like below:
You can encapsulate the method Display in Action MyDelegate as below:
An Action with one input parameter is defined in the System namespace as below:
Where in T is a type of input parameter and a T object is a value passed for the parameter.
Action with Anonymous method
You can work with Action and anonymous methods as well. You can assign an anonymous method to an Action as below:
The above code will print 9 as output.
Action with Lambda Expression
Like any other delegates, an Action can be created with a lambda expression also, as below:
The above code will also print 9 as output.
Passing Action as input parameter
You can pass an Action as a parameter of a function also. Let us say you have a class:
And two functions called Display and Show to display Name and RollNumber of Student.
Now you have a function where you need to pass either Display or Show. Or in other words you need to pass any function with the same signature of Display or Show. In that case you will be passing a delegate as an input parameter to the function.
You can call the CallingAction method in Main as below:
Above we are creating an instance of the Student class and one by one passing to the Display function and the Show function as input parameter to the CallingAction function. In the CallingAction function, we are printing the name of the function being passed as input parameter. On running you will get the following output:
I hope now you are able to answer what an Action is in simple words. I hope this post is useful. Thanks for reading.
- What is Action in C#?
- what is #pragma in c
- What is “:-!!” in C code?
- In C++, what is a memory arena?
- What is the significance of asmlinkage modifier in C?
- What is a typedef enum in Objective C?
- What is the difference between fprintf and vfprintf in C++?
- what is the difference between definition and declaration in c
- What is a meta-class in Objective-C?
- What Is C#?
- What is uadmin in Solaris?
- What is MultiSampleQuality in D3D
- what is Activity in Android
- What is 'lr_policy' in caffe?
- What is CountDownLatch in Java
- What is CyclicBarrier in Java
- What is EFF in BI
- What is HEAD in Git?
- jquery detach()和remove()的区别
- tomcat工作原理浅析
- CF422 div2 C. Hacker, pack your bags!
- android开发环境配置
- 服务器通过mavn部署项目至tomcat脚本
- What is Action in C#?
- (Java)使用Java发送带参数的http(GET)请求,获取json数据
- 简单的方法判断是不是数字
- linux调试----addr2line将(转换为地址及所在的文件及行数)
- java中的集合
- idea多模块项目发布,公共模块的代码没有改变
- iOS开发~CocoaPods使用详细说明
- 16s扩增子分析注意事项和经验总结Tips
- Android SQLite应用