C#委托(delegate)
C#中委托(delegate)是一种安全地封装方法的类型,委托是面向对象的、类型安全的。
使用委托的步骤:
1、声明委托
public delegate void DelegateHandler(string message);
2、定义委托方法
// Create a method for a delegate.
public static void DelegateMethod(string message)
{
Console.WriteLine(message);
}
3、创建委托对象,并将需要传递的函数作为参数传入
// Instantiate the delegate.
DelegateHandler handler = DelegateMethod;
或:
// Instantiate the delegate.
DelegateHandler handler = new DelegateHandler(DelegateMethod);
4、调用委托方法
// Call the delegate.
handler("Hello World");
完整示例:
using System;
using System.Collections.Generic;
using System.Text; namespace DelegateExample
{
class Program
{
public delegate void DelegateHandler(string message); public static void DelegateMethod(string message)
{
Console.WriteLine(message);
} static void Main(string[] args)
{
//DelegateHandler handler = DelegateMethod;
DelegateHandler handler = new DelegateHandler(DelegateMethod);
handler("Hello World!");
}
}
}
C#委托(delegate)的更多相关文章
- [.NET] C# 知识回顾 - 委托 delegate (续)
C# 知识回顾 - 委托 delegate (续) [博主]反骨仔 [原文]http://www.cnblogs.com/liqingwen/p/6046171.html 序 上篇<C# 知识回 ...
- [C#] C# 知识回顾 - 委托 delegate
C# 知识回顾 - 委托 delegate [博主]反骨仔 [原文]http://www.cnblogs.com/liqingwen/p/6031892.html 目录 What's 委托 委托的属性 ...
- C# 委托Delegate(一) 基础介绍&用法
本文是根据书本&网络 前人总结的. 1. 前言 定义&介绍: 委托Delegate是一个类,定义了方法的类型, 使得可以将方法当做另一个方法的参数来进行传递,这种将方法动态地赋给参数的 ...
- 为什么不能把委托(delegate)放在一个接口(interface)当中?
stackoverflow上有人问,为什么不能把委托放在一个接口当中? 投票最多的第一个答案第一句话说,“A Delegate is just another type, so you don't g ...
- C# 代理/委托 Delegate
本文转载自努力,努力,努力 1. 委托的定义:委托是函数的封装,它代表一"类"函数.他们都符合一定的签名:拥有相同的参数列表,返回值类型.同时,委托也可以看成是对函数的抽象,是函数 ...
- c# 委托 delegate
委托是一种存储函数引用的类型,在事件和事件的处理时有重要的用途 通俗的说,委托是一个可以引用方法的类型,当创建一个委托,也就创建一个引用方法的变量,进而就可以调用那个方法,即委托可以调用它所指的方法. ...
- 理解委托(delegate)及为什么要使用委托
理解委托(delegate)及为什么要使用委托 委托:是一种定义方法签名的类型. 当实例化委托时,您可以将其实例与任何具有兼容签名的方法相关联. 您可以通过委托实例调用方法. 上述为官方说法,理解起来 ...
- 深入理解委托(Delegate)
前言 委托其实一直以来都感觉自己应该挺熟悉的,直到最近又去翻了翻 CLR via C#,感觉我之前的理解可能还有失偏颇.在这记录一下. 之前文章的链接: 接口和委托的泛型可变性 C#高级编程笔记 De ...
- C# -- 使用委托 delegate 执行异步操作
C# -- 使用委托 delegate 执行异步操作 委托是一种安全地封装方法的类型,它与 C 和 C++ 中的函数指针类似. 与 C 中的函数指针不同,委托是面向对象的.类型安全的和保险的. 委托的 ...
- 委托delegate
委托delegate没有函数体.委托可以指向函数(要与指向的函数格式.类型相一致) namespace demo { public delegate double MyDelegate(double ...
随机推荐
- Cobbler学习之一--Fedora17下配置Cobbler安装环境
1:Cobbler是什么 Cobbler是一大Linux装机利器,可以快速的建立网络安装环境. 2:安装Cobbler需要的组件 createrepo httpd (apache2 for Debia ...
- 《TCP/IP高效编程:改善网络程序的44个技巧》源码在Linux上的编译
为了先完整编译通过,需要以下几个步骤: 1.进入linux子目录,执行 make.(此步骤为打补丁.) 2.返回上一级目录,打开文件 simplec.c,添加头文件 #include <stdi ...
- MySQL数据导出
1,打开命令行窗口“运行”-->输入CMD 2,进入自己MySQL Server安装目录的bin目录(我的安装目录如下) cd C:\Program Files\MySQL\MySQL Serv ...
- Python成长笔记 - 基础篇 (八)
socket编程 应用层: 表示层: 会话层: 传输层: 网络层: ip地址 数据链路层: MAC地址 物理层: 协议类型: TCP/IP协议:三次握手,四次断开 2. Socket 参数 ...
- SAP HANA专题分析目录
针对HANA的关键技术领域, 做深度解析. 1. HANA开发规范 HANA 各种对象的应用解析.版本管理,开发规范. 2. HANA系统管理 用户.系统权限.数据权限的深度解析. HANA系统配置. ...
- mySQL中删除unique key的语法
CREATE TABLE `good_booked` ( `auto_id` int(10) NOT NULL auto_increment, `good_id` int(11) default ...
- JavaScript 基础第十天
学习总结 1定时器 ①.setTimeout() // 间隔多长时间执行一次(会返回一个数字型的标识) ②.setInterval() // 间隔多长时间执行,是不断地执行. ③.clearTimeo ...
- Arc Engine下数据的加载处理
1.加载Shapefile数据 IWorkspaceFactory pWorkspaceFactory; IFeatureWorkspace pFeatureWorkspace; IFeatureLa ...
- Ubuntu NFSserver 简易安装及使用
服务器端(ip:192.168.1.100): sudo apt-get install nfs-kernel-server #安装nfs 客户端(ip:192.168.1.110): sudo ap ...
- python 实现简单排序
今天偶得一本神奇的算法秘笈,据编辑说是一本easy and intresting 的书,所以我就开始翻开了. 书中作者用的是C语言,我最近正啃python 所以想着用python来解决作者的提问. 这 ...