通过一个例子简单几种委托的使用及事件

第一章:

第一种,通过new 关键字创建对象  特别注意:这种方式必须要先有根据委托创建的方法与之关联,否则不能使用。  或者直接让委托变量直接指向某个具体的方法(前提是方法要存在)

格式为:同一命名空间下

   public delegate void One();//无参数也无返回值
 
        
    class Program
    {
        static void Main(string[] args)
        {

            One objOne0= new One(其中必须有方法作为参数);
            One objOne00=必须的方法;

        }
/////书写根据委托对象创建的方法
    }

 

第二种,匿名函数 

格式为:同一命名空间下

      public delegate void One();//无参数也无返回值
      
     public delegate void Two(string word);有参数但没有返回值

     public delegate string Three(string word);有参数也有返回值
    class Program
    {
        static void Main(string[] args)
        {
            One objOne = delegate () {方法体 };//匿名函数  无参数也无返回值

Two objTwo=delegate(string word){方法体};//有参数无返回值的匿名函数

Three objThree=delegate(string word){return word};//有参数有返回值的匿名函数 } }

第三种,lambda表达式

格式为:在同一命名空间下

 public delegate void One();//无参数也无返回值
    public delegate void Two(string word); //有参数但没有返回值
    public delegate string Three(string word);//有参数也有返回值
        
    class Program
    {
        static void Main(string[] args)
        {
            
            One objOne = () => { };//lambda 表达式 无参数也没有返回值

       
            Two objTWO=(string word)=>{ };//有参数但没有返回值的lambda表达式

     
            Three objThree = (string word) => { return word; };//有参数有返回值的lambda表达式
        }
    }

 泛型集合的lambda表达式:

1.首先创建泛型集合类并初始化

List<int> List = new List<int>(){1,2,3,4,5,6,7};

2,使用lambda表达式删除集合中的元素

list.RemoveAll(n=>n>4);

运行结果为1,2,3,4

第二章:事件

事件的由来

 

1.什么是事件?

 

2.事件能做什么?

 

posted on 2017-02-06 20:44 WFaceBoss 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/wfaceboss/p/6371744.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值