C# delegate的基本用法
c#的委托类似于c或c++的指针函数,可以将一个方法当作另一个方法的参数,进行传递。
- delegate
声明:
public delegate void OneMethod(); // 无参数,无返回值
public delegate void TwoMethod(int x,int y); //有参数,无返回值
public delegate int ThreeMethod(int x,int y); //有参数,有返回值
示例
1. xaml 文件
<Window x:Class="DelegateDemo.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:DelegateDemo"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<Button x:Name="DelegateBtn" Content="DelegateTest" HorizontalAlignment="Left" Height="42" Margin="97,92,0,0" VerticalAlignment="Top" Width="104" Click="DelegateBtn_Click"/>
</Grid>
</Window>
2. .cs 文件
namespace DelegateDemo
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
///
// delegate 声明
public delegate void OneMethod();
public delegate void TwoMethod(int x,int y);
public delegate int ThreeMethod(int x, int y);
public partial class MainWindow : Window
{
private OneMethod m_oneMethod = null;
private TwoMethod m_twoMethod = null;
private ThreeMethod m_threeMethod = null;
public MainWindow()
{
InitializeComponent();
m_oneMethod += new OneMethod( OneMethodFunc);
m_twoMethod += new TwoMethod(TwoMethodFunc);
m_threeMethod += new ThreeMethod(ThreeMethodFunc);
}
private void OneMethodFunc()
{
Console.WriteLine("OneMethodFunc");
}
private void TwoMethodFunc(int x,int y)
{
Console.WriteLine("TwoMethodFunc :{0}+{1} = {2}", x,y,x+y);
}
private int ThreeMethodFunc(int x, int y)
{
int number = x + y;
Console.WriteLine("ThreeMethodFunc :{0}+{1} = {2}", x,y, number);
return number;
}
private void DelegateBtn_Click(object sender, RoutedEventArgs e)
{
try
{
if(m_oneMethod!=null)
{
m_oneMethod();
}
if (m_twoMethod != null)
{
m_twoMethod(2,3);
}
if (m_threeMethod != null)
{
m_threeMethod(4,5);
}
}
catch(Exception error)
{
Console.WriteLine(error.Message);
}
}
}
}
运行结果
C# delegate的基本用法相关推荐
- php delegate,jQuery中delegate()方法的用法详解
delegate() 方法为指定的元素(属于被选元素的子元素)添加一个或多个事件处理程序,并规定当这些事件发生时运行的函数.下面我们具体来了解一下. 语法结构: 代码如下: $(selector).d ...
- Unity 项目中委托Delegate用法案例
Unity中Delegate的用法场景 本文提供全流程,中文翻译. Chinar 坚持将简单的生活方式,带给世人! (拥有更好的阅读体验 -- 高分辨率用户请根据需求调整网页缩放比例) Chinar ...
- C#中异步多线程的常见用法
先来看几个基本概念(纯属个人见解,可能不准确): 进程:程序运行时,占用的全部运行资源的总和. 线程:线程是隶属于操作系统管理的,也可以有自己的计算资源,是程序执行流的最小单位.任何的操作都是由线程来 ...
- c# 带返回值的action_C#委托Action、ActionT、FuncT、PredicateT
一.Action Action封装的方法没有参数也没有返回值,声明原型为: 1 public delegate void Action(); 用法如下: 1 public void Alert() 2 ...
- Spring 加载、解析applicationContext.xml 流程
概要 Spring 框架使用了BeanFactory 进行加载 xml 和生成 bean 实例.下面我们分析下Spring加载xml文件的过程. spring 版本是最新的 4.3.9 release ...
- C#委托Action、ActionT、FuncT、PredicateT
CLR环境中给我们内置了几个常用委托Action. Action<T>.Func<T>.Predicate<T>,一般我们要用到委托的时候,尽量不要自己再定义一 个 ...
- ActionT 无参数委托详解
C#委托Action.Action<T>.Func<T>.Predicate<T> CLR环境中给我们内置了几个常用委托Action. Action<T> ...
- ios开发问题汇总(一)
教程汇总 IOS比较好的网络教程:IOS编程浅蓝教程 IOS编程教程(五)自定义UITableView的表单元格 菜鸟教程-iOS教程 开发问题 实现页面切换,网上某些文章写的是: [self.nav ...
- Mr.J-- jQuery学习笔记(十一)--事件委托
jQuery事件委托 事件委托是利用事件冒泡,只指定一个事件处理程序来管理某一类型的所有事件. 为什么要用事件委托? 1.在JavaScript中添加到页面上的事件处理程序的个数直接关系到页面的整体运 ...
最新文章
- 自定义控件——轮播广告条
- ASP.NET Core + Angular 2 Template for Visual Studio
- L1-046 整除光棍 (20分)(模拟除法竖式求商的位运算)
- 物联网大数据平台软件开发架构案例解析
- 什么是mysql显错注入_十种MYSQL显错注入原理讲解(三)
- 二元函数可导与可微的关系_视频教学:期末试卷解析之多元函数基本概念及相互关系讨论...
- 使用Mono Runtime Bundle制作安装包让C#桌面应用程序脱离net framework
- 基于 Flink 打造的伴鱼实时计算平台 Palink 的设计与实现
- mysql 一对多查询组成单表字段_单表多字段MySQL模糊查询的实现
- Agreeing to the Xcode/iOS license requires admin privileges, please run “sudo xcodebuild -license”
- Office 2010 安装过程中出错
- oce专项认证 oracle_Oracle Certification Program | Oracle 中国
- USB 3.0 对无线鼠标的干扰问题
- appium远程连接---逍遥模拟器
- 社会学与计算机哪个考研容易,国内几所较热的社会学系考研难度比较
- 学python之前先掌握-成为黑客前,你首先应该掌握的Python技术
- 编写matlab语句计算试题,MATLAB测验考试题
- Linux目录结构,命令,文件类型学习
- 深度专访丨云和恩墨盖国强,识别它、抓住它,在国产数据库沸腾以前
- 磁盘IOPS和带宽(throughput)
热门文章
- Android Git 拉取代码报错:error: cannot pull with rebase: Your index contains uncommitted changes.
- Command ‘cnpm‘ not found解决方法
- Matplotlib手绘曲线
- Unity XR实现交互(抓取、移动、旋转、传送、射击)-Pico
- 闲鱼链接搭建+完整版源码后台
- 交易系统开发技能及面试之无锁编程(Lock-free)
- Kotlin协程:协程的基础与使用
- javadoc简介及用法
- spring property name=packagesToScan
- phpcms医院wap手机网站模板下载