初识委托

委托是对一类方法的抽象,这类方法有着相同的返回值和参数,我感觉委托解决的关键问题还是过于耦合度的,当然,还有事件处理。声明和使用方法如下(包括匿名方法):

None.gifusing System;
None.gif
using System.Collections.Generic;
None.gif
using System.Text;
None.gif
None.gif
namespace 委托
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif    
public delegate int mydelegate(int a,int b);  
InBlock.gif    
class Program
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
static void Main(string[] args)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            mydelegate calculate 
= new mydelegate(count.add);
InBlock.gif            calculate 
+= count.add;//匿名方法
InBlock.gif
            Console.WriteLine(calculate(52));
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

InBlock.gif    
class count
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 加法
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="a">第一个加数</param>
InBlock.gif        
/// <param name="b">第二个加数</param>
ExpandedSubBlockEnd.gif        
/// <returns>结果</returns>

InBlock.gif        public static int add(int a, int b)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
return a + b;
ExpandedSubBlockEnd.gif        }

InBlock.gif       
InBlock.gif            
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif

转载于:https://www.cnblogs.com/ATP/archive/2007/08/19/861559.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值