zoukankan      html  css  js  c++  java
  • 匿名方法

    匿名类:没有具体的名称如

      var ac=new {name=“小明”,Age=15};

      console。WriteLine(“我的名字是{0},今年{1}岁”,ac.name,ac.Age);

    匿名方法

      匿名方法,即是没有名字的方法,不能直接在类中定义,而是在给委托变量赋值的时候,需要赋值一个方法,此时可以“现做现实”,定义一个匿名方法来传值。关键字delegate()

      如果我们定义好方法可以使用委托直接调用,但是如果没有写好方法我们也可以使用匿名方法来现写一个。

    无参数的匿名:

    public delegate void addgelegate();
        class Program
        {
            static void Main(string[] args)
            {
                //调用现有的方法get()
                addgelegate my1 = get;
                //使用匿名方法来现写一个方法
                addgelegate my = delegate () { Console.WriteLine("你好中国"); };
                my();
                Console.ReadKey();
            }
    
            static void get()
            {
                Console.WriteLine("你好世界");
    
            }
        }

    无返回值有参数的匿名方法

      static void Main(string[] args)
            {
                Action<string> my = delegate (string a) 
                {
                    Console.WriteLine("你好"+a);
                };
    
                my("中国");
                Console.ReadKey();
            }

    有参数有返回值的匿名方法

     static void Main(string[] args)
            {
                Func<int, int, int> my = delegate (int a, int b)
                    {
                        return a + b;
                    };
                int c=my(3,5);
                Console.WriteLine(c);
                Console.ReadKey();
            }
  • 相关阅读:
    Ajax
    PS将图标变灰
    圆角
    前端性能优化最佳实践(转)
    jquery $(document).ready() 与window.onload的区别
    js阻止冒泡及jquery阻止事件冒泡示例介绍
    CSS选择器、CSS hack及CSS执行效率
    Github快速入门手册
    Windows 系统下Git安装图解
    史上最全github使用方法:github入门到精通
  • 原文地址:https://www.cnblogs.com/xiaowie/p/9395603.html
Copyright © 2011-2022 走看看