C#lambda表达式使用示例

分享一下有参有返回、有参无返回、无参无返回的lambda表达式使用示例

  public partial class Form1 : Form
    {
        Func<int, bool> myFunc = (in_data) => in_data == 5;//非匿名函数,myFunc,有参数和返回值
        public delegate void MyDelegate(string str);//自定义委托,有参数 无返回值
        bool flag = false;
        public Form1()
        {
            InitializeComponent();
            timer1.Start();
        }

        private void Timer1_Tick(object sender, EventArgs e)
        {
            checkBox1.Checked = myFunc(Convert.ToInt32(numericUpDown1.Value));
            //checkBox1.Checked = (bool)Invoke(myFunc, Convert.ToInt32(numericUpDown1.Value));//第二种表达

            new Thread(
                 () => { flag = Convert.ToInt32(numericUpDown1.Value) == 10 ? true : false; }
                ).Start();
            checkBox2.Checked = flag;

            new Thread(
                    (temp1) =>
                    {
                        MyDelegate mydel = (temp2) =>
                        {
                            checkBox3.Checked = temp2 == "15" ? true : false;
                        };
                        Invoke(mydel, temp1);
                    }
                ).Start(numericUpDown1.Value.ToString());

            int[] ints = { 0, 1, 2, 3, 4, 5, 6, 7, 8 };
            textBox1.Text= ints.Where((item) => item == 7).ToList()[0].ToString();//Where 为Linq表达式
        }

        private void Button1_Click(object sender, EventArgs e)
        {
            new Thread(
                 () =>//匿名函数,无返回值
                 {
                     Action action = () =>//非匿名函数,函数名为action,参数可有可无,无返回值
                     {
                         checkBox1.Checked = !checkBox1.Checked;
                     };
                     Invoke(action);
                 }
                ).Start();
           
        }
    }

 

posted @ 2020-09-23 16:21  橘猫不太胖  阅读(167)  评论(0)    收藏  举报