代表中的匿名方法 [英] Anonymous Method in Delegates

查看:168
本文介绍了代表中的匿名方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以帮我把这个Delegate程序方法转换成匿名方法吗?

Hi , Can some one help me to convert this Delegate program method to anonymous method

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Delgates_Ex1
{

    public delegate void ExampleDelgate(int x, int y);


    class Democlass
    {
        public void Add(int x, int y)
        {

            Console.WriteLine(x+y);
            

        }

    }


    class Program
    {
      


        static void Main(string[] args)
        {
            Democlass d = new Democlass();

            ExampleDelgate e1 = new ExampleDelgate(d.Add); 


            e1(20, 10);  
        }
    }
}

推荐答案

您在这里(请参阅 ^ ])
Here you are (see MSDN[^])
static void Main(string[] args)
{
  Democlass d = new Democlass();

  ExampleDelgate e1 = new ExampleDelgate(d.Add);
  ExampleDelgate e2 = delegate(int x, int y){ Console.WriteLine(x+y);}; // anonymous method

  e1(20, 10);
  e2(20, 10);
}


我们不做您的作业:它的设置是有原因的.在这里,您可以考虑自己被告知的内容,并尝试理解它.也可以在那里帮助您的导师识别您的弱点,并将更多的注意力放在补救措施上.

自己尝试,您可能会发现它并不像您想的那么困难!
We do not do your homework: it is set for a reason. It is there so that you think about what you have been told, and try to understand it. It is also there so that your tutor can identify areas where you are weak, and focus more attention on remedial action.

Try it yourself, you may find it is not as difficult as you think!


这篇关于代表中的匿名方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆