惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

K
Kaspersky official blog
Martin Fowler
Martin Fowler
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
V
Visual Studio Blog
博客园_首页
Engineering at Meta
Engineering at Meta
The Cloudflare Blog
MongoDB | Blog
MongoDB | Blog
Blog — PlanetScale
Blog — PlanetScale
T
The Blog of Author Tim Ferriss
雷峰网
雷峰网
D
Docker
博客园 - 司徒正美
S
SegmentFault 最新的问题
M
MIT News - Artificial intelligence
博客园 - 叶小钗
博客园 - 三生石上(FineUI控件)
U
Unit 42
J
Java Code Geeks
A
About on SuperTechFans
N
Netflix TechBlog - Medium
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
S
Security Affairs
I
Intezer
Cisco Talos Blog
Cisco Talos Blog
C
Cyber Attacks, Cyber Crime and Cyber Security
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
B
Blog RSS Feed
P
Privacy & Cybersecurity Law Blog
T
Tenable Blog
T
Threatpost
H
Hacker News: Front Page
G
Google Developers Blog
博客园 - 【当耐特】
Hugging Face - Blog
Hugging Face - Blog
Apple Machine Learning Research
Apple Machine Learning Research
L
Lohrmann on Cybersecurity
大猫的无限游戏
大猫的无限游戏
Google DeepMind News
Google DeepMind News
A
Arctic Wolf
S
Secure Thoughts
GbyAI
GbyAI
NISL@THU
NISL@THU
S
Security @ Cisco Blogs
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
Webroot Blog
Webroot Blog
C
CXSECURITY Database RSS Feed - CXSecurity.com
O
OpenAI News
Spread Privacy
Spread Privacy
Application and Cybersecurity Blog
Application and Cybersecurity Blog

博客园 - 默默無語中

类与结构的区别 GridView分页-编辑-删除 DataList修改删除 - 默默無語中 - 博客园 弄了一晚上研究连接字符串,给自己留个清楚的回忆~! - 默默無語中 - 博客园 [数据库连接字符串] SQL Server 2005 Compact Edition 连接字符串 委托的笔记(整理) [导入]asp.net高级教程(三)-实战篇 [导入]asp.net高级教程(三)-对象 [导入]asp.net高级教程(五)-实战篇(中) [导入].NET之ASPWebApplication快速入门(3) [导入].NET之ASPWebApplication快速入门(2) [导入].NET之ASPWebApplication快速入门(1) [导入].NET之ASPWebApplication快速入门(4) [导入].NET之ASPWebApplication快速入门(5) [导入]asp.NET特写 [导入]亲密接触ASP.Net(1) [导入]亲密接触ASP.Net(3) [导入]亲密接触ASP.Net(4) [导入]亲密接触ASP.Net(2) [导入]亲密接触ASP.Net(6) [导入]亲密接触ASP.Net(5)
委托对象(收集)
默默無語中 · 2008-05-11 · via 博客园 - 默默無語中
     

最近看到一篇来自Arul Chinnappan扼要明了的委托介绍,翻译了一下和大家共享,翻译水平和时间都有限,大家见谅。

委托的定义:

委托是一种在对象里保存方法引用的类型,同时也是一种类型安全的函数指针。

委托的优点:

压缩方法的调用。

合理有效地使用委托能提升应用程序的性能。

用于调用匿名方法。

委托的声明:

委托应使用public delegate type_of_delegate delegate_name()的形式来声明。
示例:public delegate int mydelegate(int delvar1,int delvar2)

注意点:可以在不带参数或参数列表的情况下声明委托。

        应当遵循和声明方法一样的语法来声明委托。

使用委托的示例程序:

public delegate double Delegate_Prod(int a,int b);

class Class1
 
{

static double fn_Prodvalues(int val1,int val2)
         
{
          
return val1*val2;
          }

static void Main(string[] args)
 
{   

               
//Creating the Delegate Instance
               Delegate_Prod delObj = new Delegate_Prod(fn_Prodvalues);
              Console.Write(
"Please Enter Values");

              
int v1 = Int32.Parse(Console.ReadLine());
               
int v2 = Int32.Parse(Console.ReadLine());

               
//use a delegate for processing

               
double res = delObj(v1,v2);
               Console.WriteLine (
"Result :"+res);
               Console.ReadLine();

}

 }

示例程序解析:

上面我用一段小程序示范了委托的使用。委托Delegate_Prod声明时指定了两个只接受整型变量的返回类型。同样类中名为fn_Prodvalues的方法也是如此,委托和方法具有相同的签名和参数类型。

Main方法中创建一个委托实例并用如下方式将函数名称传递给该委托实例:

Delegate_Prod delObj = new Delegate_Prod(fn_Prodvalues);

这样我们就接受了来自用户的两个值并将其传递给委托:

delObj(v1,v2);

在此委托对象压缩了方法的功能并返回我们在方法中指定的结果。

多播委托:

多播委托包含一个以上方法的引用。

多播委托包含的方法必须返回void,否则会抛出run-time exception

使用多播委托的示例程序:

delegate void Delegate_Multicast(int x, int y);

Class Class2

{
static void Method1(int x, int y) {
  Console.WriteLine(
"You r in Method 1");
}

static void Method2(int x, int y) {
  Console.WriteLine(
"You r in Method 2");
}

public static void Main() 
{
  Delegate_Multicast func 
= new Delegate_Multicast(Method1);

func 
+= new Delegate_Multicast(Method2);
      func(
1,2);             // Method1 and Method2 are called
      func -= new Delegate_Multicast(Method1);
      func(
2,3);             // Only Method2 is called
   }


}
   

示例程序解析:

大家可以看到上面的示例程序分别定义了名为method1 method2的两个接受整型参数、返回类型为void的方法。

Main函数里使用下面的声明创建委托对象:

Delegate_Multicast func = new Delegate_Multicast(Method1);

然后使用+= 来添加委托,使用-=来移除委托。

总结:

这篇小文章将帮助您更好地理解委托。