Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the Action delegate in the C # built-in generic delegate

2025-04-05 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

Editor to share with you what the Action delegate in C# built-in generic delegation is, I believe most people don't know much about it, so share this article for your reference. I hope you can learn a lot after reading this article. Let's learn about it together.

1. What is an Action generic delegate

Action is a generic delegate built into the .NET Framework, and you can use an Action delegate to pass methods as parameters without displaying a delegate that declares customization. The encapsulated method must correspond to the method signature defined by this delegate. That is, the encapsulated method must have a parameter passed to it by value, and there must be no return value.

2. Action delegate definition

View the definition of Action:

Using System.Runtime.CompilerServices;namespace System {/ Summary: / / encapsulates a method that has no parameters and does not return a value. [TypeForwardedFrom ("System.Core, Version=3.5.0.0, Culture=Neutral, PublicKeyToken=b77a5c561934e089")] public delegate void Action ();}

You will find that Action is actually a delegate with no return value.

3. Example

Action delegate at least 0 parameters, up to 16 parameters, no return value.

Action represents a delegate with no parameters and no return value.

Action represents a delegate with passed parameter int,string and no return value.

Action represents a delegate with passed parameter int,string,bool and no return value.

Action indicates a delegate with 4 int parameters passed in and no return value.

The code example is as follows:

Using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace ActionDemo {class Program {static void Main (string [] args) {/ / delegate with no parameters and no return value Action action1 = new Action (ActionWithNoParaNoReturn); action1 () Console.WriteLine ("- -"); / / use delegate Action action2 = delegate {Console.WriteLine ("here is using delegate");}; / / execute action2 () Console.WriteLine ("- -"); / / use anonymous delegate Action action3 = () = > {Console.WriteLine ("this is anonymous delegate");}; action3 () Console.WriteLine ("- -"); / / delegate Action action4 = new Action (ActionWithPara) with parameters but no return value; action4 (23); Console.WriteLine ("- -") / / use delegate Action action5 = delegate (int I) {Console.WriteLine ($"here is the delegate using delegate, the parameter value is: {I}");}; action5 (45); Console.WriteLine ("-") / / use anonymous delegate Action action6 = (string s) = > {Console.WriteLine ($"here is anonymous delegate, parameter value is: {s}");}; action6 ("345s"); Console.WriteLine ("- -") / / delegate Action action7 = new Action (ActionWithMulitPara) with no return value for multiple parameters; action7 (7, "abc"); Console.WriteLine ("- -") / use delegate Action action8 = delegate (int i1, int i2, string s) {Console.WriteLine ($"here is the Action delegate of three parameters, parameter 1 is: {i1}, parameter 2 is: {i2}, parameter 3 is: {s}");}; action8 (12, 34, "abc") Console.WriteLine ("-") Action action9 = (int i1dint i2, string s1jol string S2) = > {Console.WriteLine ($"here is the delegate with four parameters, parameter 1 is: {i1}, parameter 2 is: {i2}, parameter 3 is: {S1}, parameter 4 is: {S2}");} / / execute delegate action9 (34 abc 56, "abc", "def"); Console.ReadKey ();} static void ActionWithNoParaNoReturn () {Console.WriteLine ("this is an Action delegate with no parameters and no return value") } static void ActionWithPara (int I) {Console.WriteLine ($"here is the delegate with parameter but no return value, parameter value is: {I}") } static void ActionWithMulitPara (int iForce string s) {Console.WriteLine ($"here is a delegate with two parameters without a return value. The value of parameter 1 is: {I}, the value of parameter 2 is: {s}");}

Running result:

4. Real examples

Take a look at the following screenshot first:

You can see from the screenshot that the parameter of the ForEach () method is an Action delegate with no return value of parameter type T, which is passed to the ForEach () method using the Action delegate as a parameter in the following example.

1. Define Student entity class

Using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace ActionDemo {public class Student {public int Id {get; set;} public string Name {get; set;} public int Age {get; set;} public int Sex {get; set;}

2. Use ForEach () method to output the contents of the collection.

Using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks Namespace ActionDemo {public class ActionTest {public static void Test () {List list = new List () {new Student () {Id=1,Name= "Zhang San", Age=19,Sex=1}, new Student () {Id=2,Name= "Li Si", Age=20,Sex=2}, new Student () {Id=3,Name= "Wang Wu", Age=23,Sex=1} New Student () {Id=4,Name= "Zhao Liu", Age=18,Sex=1}} / / the Action delegate is passed as a parameter to the ForEach () method list.ForEach (student = > {Console.WriteLine ($"name: {student.Name}, age: {student.Age}");});}}

3. Call in the Main () method

ActionTest.Test ()

4. Results

These are all the contents of the article "what is the Action delegate in the C# built-in generic delegate". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report