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();
        }

}

上一篇:Gearman使用示例


下一篇:flowable实战(一)flowable与spring boot集成