C#,Lambda表达式实例

下面是三个在wimform 下的三个Lambda表达式实例


    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {//lambda表达式调用
            //md();
            //md2("单参数Lambda表达式");
           int count= md3(10, 20, 30);
            MessageBox.Show(count.ToString());
        }
        private delegate void MyDelegate();//无参
        MyDelegate md = () => { MessageBox.Show("无参无返回值Lambda"); };

        private delegate void MyDelegate2(string msg);
        MyDelegate2 md2 = m => { MessageBox.Show("有参" + m); };//单个参数可以省掉小括号

        private delegate int MyDelegate3(params int[] par);//多参
        MyDelegate3 md3 = (arr) => {
            int count=0;
            foreach(int i in arr)
            {
                count += i;
            }
            return count;
        };
    }


评论

  1. 120.239.196.*2018-07-04 07:58
    找了好久 终于找到完美的代码