经验总结36--C#匿名(事件,对象...)

来源:互联网 发布:软件信息龙头股 编辑:程序博客网 时间:2024/05/23 13:13

有时候代码方便,就会使用匿名的东西。

1、匿名事件

args.CookieGot += (s, e) =>
                {
                    this.IsWebLogon = true;
                };

不用专门再去写方法,当然这种方式简介的时候使用。

2、匿名对象

var ohList = ohManager.GetList().Select(x => new { x.ID, x.Name,x.CreateTime });

用户接口或前后台交互,指定某些字段,且新的对象,不用再去建实体。

3、匿名Form线程

this.Invoke(new Action(() =>
            {
                Btc38ImageForm form = new Btc38ImageForm();
                form.ShowDialog();
            }));

防止影响主form,不会卡。

4、匿名线程

Thread t = new Thread(() =>
            {
                Runing();
            });
            t.IsBackground = true;//程序关闭,线程关闭
            t.Start();

5、匿名timer

timer = new System.Timers.Timer(1 * 1000);
                timer.Elapsed += (s, e) => {
                    d1.Runing();
                    d15.Runing();
                };
                timer.AutoReset = true;
                timer.Enabled = true;
                timer.Start();

类似匿名事件。

0 0
原创粉丝点击