zoukankan      html  css  js  c++  java
  • action func用法记记

    public partial class Form1 : Form

        {

            public Form1()

            {

                InitializeComponent();

            }

            public delegate void showMessage(string msg);

            public static void showM(string m)

            {

                MessageBox.Show(m);

            }

            public string returnM(string m)

            {

                return m;

            }

            private void button1_Click(object sender, EventArgs e)

            {

                //3

                showMessage s = new showMessage(showM);

                s("xxx");

                //1

                Action<string> action = showM;

                action("123");

                //2

                Action<string> a = X => showM(X);

                a("aaaaaaaaaa");

                //6

                Action<string> ac = (x1) =>

                {

                    MessageBox.Show(x1);

                };

                ac("111111111111111");

                //6

                Action<string> ac2 = delegate(string xx){

                    MessageBox.Show(xx);

                };

                ac2("222222222222");

               

                //5

                string m1 = "123";

                textBox1.Invoke((Action<string>)delegate(string m)

                {

                    textBox1.Text = m;

                },m1);

                textBox1.Invoke(new Action<string>((x4) =>

                {

                    textBox1.Text = x4;

                }), "aaaaax4");

     

                //4 --需要return

                Func<string, string> f = returnM;

                MessageBox.Show(f("abc"));

                //7

                Func<string> func = delegate()

                {

                    return "我是Func<TResult>委托出来的结果";

                };

                MessageBox.Show(func());

                //8

                Func<string, string> funcOne = delegate(string s3)

                {

                    return s3;

                };

                MessageBox.Show(funcOne("我是Func<T,TResult>委托出来的结果"));

     

                //9

                Func<string, string, string> funcTwo = delegate(string value1, string value2)

                {

                    return value1 + " " + value2;

                };

                MessageBox.Show(funcTwo("xxx", "我是Func<T,TResult>委托出来的结果"));

                //10

                Func<string, string, string> ff = (x1, x2) =>

                {

                    return x1 + " " + x2;

                };

                MessageBox.Show(funcTwo("xxx", "我是Func<T,TResult>委托出来的结果"));

            }

     

     

    public IList<Model> ToList(DataTable dt, Func<DataRow, Model> func)
            {
                if (dt == null || dt.Rows.Count == 0)
                {
                    return null;
                }
                IList<Model> list = new List<Model>(dt.Rows.Count);
                foreach (DataRow dr in dt.Rows)
                {
                    list.Add(func(dr));
                }
                return list;
            } 
            protected void Page_Load(object sender, EventArgs e)
            {
                if (!this.IsPostBack)
                {
                    DataTable dt = newstable();
                    IList<Model> list = ToList(dt, delegate(DataRow row)
                    {
                        Model m = new Model();
                        m.f_id =Convert.ToInt32(row["f_id"]);
                        return m;
                    });  
                }
            }
    //////////

      static void Main(string[] args)
            {
                IList<String> list = new List<String>();
                list.Add("1");
                list.Add("2");
                list.Add("3");
                list.Add("4");
                list.Add("5");
                Func<String, bool> f = x => x.Equals("2");
                IList<String> list2 = filter(f, list);
            }
            static IList<String> filter(Func<String, bool> exp, IList<String> list)
            {
                return list.Where(exp).ToList();
            }

     

     

        }

  • 相关阅读:
    python自动化测试-使用第三方python库技术实现
    JMeter目录结构
    JMeter Http请求之content-type用法
    JMeter生成性能报表-Windows环境和Linux环境
    JMeter4.0 IF Controller
    c++ 初始化列表和构造函数初始化区别
    关于C++ 中 thread 的拷贝构造函数
    函数的参数类型 指针和指针的引用的区别
    window10 vs2013 SIFTGPU
    Qt使用双缓冲绘图时报错:pure virtual method called
  • 原文地址:https://www.cnblogs.com/KimhillZhang/p/3308184.html
Copyright © 2011-2022 走看看