C# 基础19 匿名方法+Lambda表达式+扩展方法

匿名方法

匿名方法(Anonymous methods)提供了一种传递代码块作为委托参数的技术。匿名方法是没有名称只有主体的方法 。

在匿名方法中不需要指定返回类型,它是从方法主体内的return语句推断的。

匿名方法语法:

                                   delegate (参数) {方法体};

匿名方法特点: 

1.没有方法名(由系统默认名字)

2.只能在定义的时候调用,其他地方无法调用

匿名方法用处:

当你需要一个临时方法,该方法使用次数极少或者你需要方法代码很简短。

匿名方法使用注意:

1.匿名方法不能访问外部方法的ref和out参数

2.匿名方法内的局部变量不可以与外部方法的局部变量是重名,否则编译会报重定义错误。

3.匿名方法可以访问外部方法定义的局部变量。

4.只能用作委托方法绑定。

代码示例:

    class Program
    {
        public delegate void Del();         //创建委托
        static void Main(string[] args)
        {
            Del F = delegate()              //实例=delegate
              {
                  Console.WriteLine("小鸡炖蘑菇");
              };
            F();                            //直接调用
            FF1();
            Console.ReadKey();
        }
        static void FF1()                    //直接调用
        {
            Console.WriteLine("天王盖地虎");
        }
    } 
----------------------------------------------------------------->
小鸡炖蘑菇
天王盖地虎

委托可以通过匿名方法调用,也可以通过命名方法调用,即通过向委托对象传递方法参数。

    class Program
    {
        public delegate void Del(string a);//声明委托
        static string b = "qwe";//变量
        static void Main(string[] args)
        {
            Del del = delegate (string c)        //使用匿名方法创建委托实例
              {
                  Console.WriteLine(c);
              };
            del("小鸡炖蘑菇");
            Console.ReadKey();
        }
        static void FF1()        //静态方法
        {
            Console.WriteLine(b+"天王盖地虎");   //输出
        }
    } 
--------------------------------------------------------------------

            del = new Del(FF1);                  //使用命名方法实例化委托
            del("宝塔镇河妖");
            Console.ReadKey();
        }
        static void FF1(string n)        //静态方法
        {
            Console.WriteLine(b+"天王盖地虎");   //输出
        }
----------------------------------------------------------------------

Lambda表达式

Lambda表达式就是匿名方法演变过来的,是一种可用于创建委托的匿名函数。

语法格式:

=>"转到"

Lambda与匿名方法的一般规则如下:

1.参数数量必须与委托类型的参数数量相同;

2.每个输入的参数必须都能够隐式转换为委托的参数类型;

3.返回值(如果有)必须能够隐式转换为委托的返回类型。

代码示例

  无返回值无参数 
 class Program
    {
        public delegate void DEl();
        static void Main(string[] args)
        {
            DEl dEl;
            dEl = () => { Console.WriteLine("一二三四五"); };   //Lambda表达式
            //dEl = () => Console.WriteLine("上山打老虎");      //方法体只有一行时可简化
            dEl();
            Console.ReadKey();
        }
    }
无返回值含参数
 class Program
    {
        public delegate void DEl(int a,string b);
        static void Main(string[] args)
        {
            DEl dEl;
            dEl = (int a, string b) => { Console.WriteLine("一二三四五"); };
            //dEl = (int a, string b) => Console.WriteLine("上山打老虎");  //简化1
            //dEl = (a, b) => Console.WriteLine("老虎没打着");             //简化2
            dEl(1,"w");
            Console.ReadKey();
        }
    }
有返回值含一个参数,方法体只有return语句
    class Program
    {
        public delegate int DEl(string b);
        static void Main(string[] args)
        {
            DEl dEl;
            dEl = (string a) => { return 1; };
            //dEl = (s) => { return 1; };           //简化1
            //dEl = a => 1;                         //简化2
            dEl("www");
            Console.ReadKey();
        }
    }
有返回值含多个参数,方法体自由return语句
    class Program
    {
        public delegate int DEl(string b,int a);
        static void Main(string[] args)
        {
            DEl dEl;
            dEl = (string a, int b) => { return 1; };
            dEl = (a, b) => { return 1; };            
            dEl = (a,b) => 1;                 //简化1
            dEl("qwe",1);                     //简化2
            Console.ReadKey();
        }
    }
有返回值含多个参数,方法体有多行语句
    class Program
    {
        public delegate int DEl(string b,int a);
        static void Main(string[] args)
        {
            DEl dEl;
            dEl = (string a, int b) => { Console.WriteLine("一二三四五");return 1; };
           // dEl = (a, b) => { Console.WriteLine("一二三四五");return 1; };       //简化1
            dEl("qwe",1);
            Console.ReadKey();
        }
    }

扩展方法

扩展方法能够向现有类型“添加”方法,而无需创建新的派生类型,重新编译或以其他方式修改原始类型。

语法要求:

  1. 此方法必须是一个静态方法
  2. 此方法必须放在静态类中
  3. 此方法的第一个参数必须以this开头,并且制定此方法是扩展自哪个类

扩展方法的特点

  1. 扩展方法扩展自哪个类,就必须是此类型的变量来使用,其他类型无法使用;
  2. 拓展方法中的this后面的参数不属于方法的参数;
  3. 如果扩展方法和实例方法具有相同的签名,则优先调用实例方法;
  4. 扩展自父类上的方法,可以被子类的对象直接使用;
  5. 扩展方法最终还是被编译器议成:静态类,静态方法();
 class Program
    {
        static void Main(string[] args)
        {
            P1 p1 = new P1();
            p1.Sc();
            Console.ReadKey();
        }
    }
    class P1
    {

    }
    static class P2             //静态类
    {
        public static void Sc(this P1 p1)     //静态方法
        {
            Console.WriteLine("好好");
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            string a = "qwe";
            a.Sc("asd");
            Console.ReadKey();
        }
    }
   static class P1
    {
        public static void Sc(this string a,string b)
        {
            int c = b.Length;
            Console.WriteLine(c);
        }
    }
--------------------------------------------------------
返回3

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值