zoukankan      html  css  js  c++  java
  • 委托

    一、初识委托
        1、C#中委托是一种特殊的引用类型,委托相当于方法的一个指针,委托和类是同一级别的
        2、语法:
            访问修饰符 delegate 返回类型 名称(参数列表);
            
            eg:
              

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 using System.Threading.Tasks;
     6 
     7 namespace ConsoleApplication1
     8 {
     9     public delegate double multiOp(double x);
    10     class Program
    11     {
    12         //方法1  求平方
    13         static double Multiply(double x)
    14         {
    15             return x * x;
    16         }
    17         //方法2 求开方
    18         static double Sqrt(double x)
    19         {
    20             return Math.Sqrt(x);
    21         }
    22 
    23         //方法3 委托作为方法的参数
    24         static double Operation(double x, multiOp ops)
    25         {
    26             return ops(x);
    27         }
    28 
    29         static void Main(string[] args)
    30         {
    31             //实例化了一个委托对象
    32                 //实例化委托并调用
    33                 multiOp compute = new multiOp(Multiply);
    34                 Console.WriteLine("{0}的平方为{1}",4,compute(4));
    35 
    36                 //实例化委托并调用
    37                 compute = new multiOp(Sqrt);
    38                 Console.WriteLine("{0}的开方为{1}", 16, compute(16));
    39 
    40             //不实例化委托
    41                 multiOp compute2 = Multiply;
    42                 Console.WriteLine("{0}的平方为{1}", 4, compute2(4));
    43 
    44                 compute2 = Sqrt;
    45                 Console.WriteLine("{0}的开方为{1}", 16, compute2(16));
    46 
    47             //委托作为方法的参数
    48                 Console.WriteLine("委托作为方法的参数:" + Operation(2, Multiply)); 
    49 
    50             Console.ReadLine();
    51         }
    52     }
    53 }


                
    二、匿名方法
        1、匿名方法这种语法机制主要是为了简化委托对象的使用
        2、语法:    
            delegate(参数列表){表达式或语句块}
            
            eg:
              

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 using System.Threading.Tasks;
     6 
     7 namespace ConsoleApplication2
     8 {
     9     public delegate double multiOp(double x);
    10     class Program2
    11     {
    12         //方法 委托作为方法的参数
    13         static double Operation(double x, multiOp ops)
    14         {
    15             return ops(x);
    16         }
    17 
    18         static void Main(string[] args)
    19         {
    20             multiOp mp = delegate(double x)
    21             {
    22                 return x * x;
    23             };
    24 
    25             //1.将方法参数作为参数
    26             Console.WriteLine("委托作为方法的参数:" + Operation(2, mp));
    27             //2.将方法参数作为参数
    28             Console.WriteLine("委托作为方法的参数:" + Operation(2, delegate(double x) { return x * x; }));
    29 
    30             Console.ReadLine();
    31             
    32         }
    33     }
    34 }



    三、Lamda表达式
        1、在C#3.0提供了Lamda表达式,进一步简化了委托的使用
        2、语法:    
            (参数列表)=>表达式或语句块
            
            (x) =>{return x*x}
            (x) =>{return r=x*x;return r;}
            
            参数列表:可以是多个参数,一个参数,或者无参数;必须和委托参数一致
            =>:运算符,表明什么参数传递给表达式
            表达式或语句块:方法的实现部分(方法体)
            
            eg:
              

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 using System.Threading.Tasks;
     6 
     7 namespace ConsoleApplication3
     8 {
     9     public delegate double multiOp(double x);
    10     class Program3
    11     {
    12         //方法 委托作为方法的参数
    13         static double Operation(double x, multiOp ops)
    14         {
    15             return ops(x);
    16         }
    17 
    18         static void Main(string[] args)
    19         {
    20             //1.将方法参数作为参数
    21             Console.WriteLine("委托作为方法的参数:" + Operation(2, (x) => x * x));
    22 
    23             Console.ReadLine();
    24         }
    25     }
    26 }


                
    四、泛型委托
        1、在.NET中,提供了一些默认的委托形式
            public delegate TResult Func<in T,out TResult>(T arg);            //最后一个是输出类型其他都是输入类型
                                    Action<in T,in T1>(T arg);                //全是输入类型
                                    Predicate<in T>(T arg);
            eg:
              

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 using System.Threading.Tasks;
     6 
     7 namespace ConsoleApplication4
     8 {
     9     class Program4
    10     {
    11         static void Main(string[] args)
    12         {
    13             Func<double, double> ope = x => x * x;
    14             Func<int, bool> del = a => a > 10;
    15             
    16 
    17             Console.WriteLine(ope(10));
    18             Console.WriteLine(del(12));
    19         }
    20     }
    21 }
  • 相关阅读:
    Asp.net 表单打印时的样式设置【原】
    Asp.net页面编辑时怎样获取隐藏列的值【原】
    C# Winform 捕获窗体的最小化和最大化事件、关闭按钮事件【整理】
    SQL 快速删除表数据并不生成日志的语句【搜藏】
    Asp.net 前段页面控件 filedset 对应的服务端控件【原】
    Asp.net 关于 HTTP403 – 访问错误 的原因及解决【原】
    Js对一个对象应用滤镜的几种方法【整理】
    Asp.net 查找不到动态创建的控件之解决办法【整理】
    C# windows service承載遠程對象
    自己写的一个日志记录类
  • 原文地址:https://www.cnblogs.com/JamelAr/p/7123036.html
Copyright © 2011-2022 走看看