What is Action in C#?
2017-07-03 13:33
453 查看
转的,没有中文版,别怪我啊....
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.
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 else is new in C# 5?
- What is the fastest, case insensitive, way to see if a string contains another string in C#?
- C#.NET Array扩展 Join/Compress/Decompress/Deserialize/IsInArray/Action
- what is differences between action and actionlistener in JSF components?
- Part 68 - What is the use of NonAction attribute in mvc
- what is virtual method in dotnet? (什么是虚方法)
- What ’ s in the .NET Framework?[摘自Beginning Visual C# 2008]
- What is the C# equivalent to C++ OutputDebugString?
- [原创]What is "Type" in managed heap?
- What is the most important thing in your life?
- In TCP networking, what is a FIN_WAIT state?
- LINQ in Action 一书第二章翻译原稿(第二章C# and VB.NET语言增强)
- After Java and C# - what is next?
- macro与inline的区别 What is the difference between macro and inline?
- What do you think is the role of parents in preventing crime amongst young people?
- It is said that wars in the 21st century will be fought over water. Do you agree? What do you think can be done now to alleviate
- .NET 4.0 will come soon, let’s review what is new in .net 3.0
- What is the ARGB int encoding of pixels in Java's AWT?
- what is new in c sharp 2.0--study from msdn [转]
- What is "Type" in managed heap?