C# Action 委托
来源:互联网 发布:怎么关掉淘宝店铺 编辑:程序博客网 时间:2024/06/06 12:50
C# Action 委托
1.显示的 delegate
首先我们先看一个没有用Action的委托
using System;using System.Windows.Forms;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;delegate void ShowWindowsMessageDelegate(string message);namespace testDelegate{ class Program { public static void Main() { ShowWindowsMessageDelegate showWindowsMessageDelegate; showWindowsMessageDelegate = OnShow; showWindowsMessageDelegate("Show Windows Message Success!"); } private static void OnShow(string message) { MessageBox.Show(message); } }}代码 很简单, 定义了一个显示窗口的代理, 用于显示一个窗口和一段文字
2.Action<in T> 委托
using System;using System.Windows.Forms;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace testDelegate{ class Program { public static void Main() { Action<string> ShowWindowsMessage; ShowWindowsMessage = OnShow; ShowWindowsMessage("Show Windows Message Success!"); } private static void OnShow(string message) { MessageBox.Show(message); } }}先看下Action<T>的描述
哦~~~ 原来Action<T>就是一个参数为T 的无返回值泛型委托
3.Action<in T1,in T2> 委托
那么两个参数的就自然想到了 Action<T1,T2> using System;using System.Windows.Forms;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;public class TestAction1{ public static void Main() { Action<string, int> LogMessageByTimes; LogMessageByTimes = OnLogByTimes; OnLogByTimes("Log By Times Success!", 3); } private static void OnLogByTimes(string message, int times) { for (int i = 0; i < times; i++) { Console.WriteLine(message); } }}运行结果
4.ForEach 委托
using System;using System.Windows.Forms;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;class Program{ static void Main() { List<String> names = new List<String>(); names.Add("ForEach"); names.Add("is"); names.Add("Action<T> action"); names.Add("Actually"); // 参看ForEach 的描述 public void ForEach(Action<T> action); // 参数 action 要对 System.Collections.Generic.List<T> 的每个元素执行的 System.Action<T> 委托。 // 所以 names.ForEach(Print); 这句代码其实也是使用的 Action<T> delegate names.ForEach(Print); } private static void Print(string s) { Console.WriteLine(s); }}
5.匿名方法作为委托参数
using System;using System.Windows.Forms;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;public class TestAnonMethod{ public static void Main() { Action<string> ShowWindowsMessage; ShowWindowsMessage = delegate(string s) { OnShow(s); }; ShowWindowsMessage("Show Windows Message Success!"); } private static void OnShow(string message) { MessageBox.Show(message); }}
0 0
- C# Action<T> 委托
- C# Action 委托
- C# Action<T> 委托
- C# Action委托方法
- C# Action委托类型
- C#中 --- Action 委托
- C# 委托 delegate Action
- C#委托之 Action<T>委托
- C# Action 委托 + lambda 表达式
- c#委托Action和Func
- C# Action和Func委托
- C#的委托区别 Action,Func, Predicate
- C#的委托 Action<>和Func<>
- C#委托类型-(Func,Action,Predicate,lambda)
- C# 泛型委托 Action/Func
- C#笔记 委托 事件 匿名方法 Action
- C# 委托(Delegate Action Func Predicate)
- C#委托变量Action和Func
- Git常用命令归档
- string.format大全
- 解决WebView调用loadData()方法显示乱码的问题
- Tomcat – Java.Lang.OutOfMemoryError: PermGen Space
- xhost display 详解
- C# Action 委托
- epoptes ubuntu 电子教学系统 电子教室 远程控制
- 三代DeepID简介
- Spark应用中的日志
- JAVA设计模式(2) —<创建型>抽象工厂模式(Abstract Factory)
- spark streaming源码分析5 checkpoint
- UIViewController不dealloc問題
- The Constrained Application Protocol (CoAP)协议翻译
- ViewPage滑动背景渐变效果的Splash主页面