C#2.0技术探讨(1):匿名方法
2007-12-10 23:51
381 查看
/*--===------------------------------------------===---
匿名方法: 通过+= 后面跟delegate{}; 以内联方式编码
匿名方法是.NET2.0特有的,而不是1.1具有的(给学员演示的时候要注意)
许明会 2007年12月10日 23:35:30
--===------------------------------------------===---*/
using System;
using System.Windows.Forms;
namespace xumh
{
public class myClass:Form
{
private Button btnHello;
private Label lblTime;
private Timer tmr;
public myClass()
{
btnHello = new Button();
btnHello.Text = "问好";
btnHello.Click += delegate{ //测试匿名方法
MessageBox.Show("Yes,It's Anonymouse method!");
};
btnHello.Location = new System.Drawing.Point(100,80);
this.Controls.Add(btnHello);
//lblTime
lblTime = new Label();
lblTime.Location = new System.Drawing.Point(100,120);
lblTime.Size = new System.Drawing.Size(140,20);
this.Controls.Add(lblTime);
//tmr
tmr = new Timer();
tmr.Enabled = true;
tmr.Interval = 1000;
tmr.Tick += delegate{ //测试匿名方法
lblTime.Text = DateTime.Now.ToString();
};
//带参数的匿名方法
this.MouseMove += delegate(object Sender,MouseEventArgs e){
this.Text = string.Format("({0},{1})",e.X,e.Y);
this.Opacity = double.Parse( e.X.ToString()) /this.Width;
};
}
};
public class runMyApp
{
static void Main()
{
Application.Run(new myClass());
}
};
}
匿名方法: 通过+= 后面跟delegate{}; 以内联方式编码
匿名方法是.NET2.0特有的,而不是1.1具有的(给学员演示的时候要注意)
许明会 2007年12月10日 23:35:30
--===------------------------------------------===---*/
using System;
using System.Windows.Forms;
namespace xumh
{
public class myClass:Form
{
private Button btnHello;
private Label lblTime;
private Timer tmr;
public myClass()
{
btnHello = new Button();
btnHello.Text = "问好";
btnHello.Click += delegate{ //测试匿名方法
MessageBox.Show("Yes,It's Anonymouse method!");
};
btnHello.Location = new System.Drawing.Point(100,80);
this.Controls.Add(btnHello);
//lblTime
lblTime = new Label();
lblTime.Location = new System.Drawing.Point(100,120);
lblTime.Size = new System.Drawing.Size(140,20);
this.Controls.Add(lblTime);
//tmr
tmr = new Timer();
tmr.Enabled = true;
tmr.Interval = 1000;
tmr.Tick += delegate{ //测试匿名方法
lblTime.Text = DateTime.Now.ToString();
};
//带参数的匿名方法
this.MouseMove += delegate(object Sender,MouseEventArgs e){
this.Text = string.Format("({0},{1})",e.X,e.Y);
this.Opacity = double.Parse( e.X.ToString()) /this.Width;
};
}
};
public class runMyApp
{
static void Main()
{
Application.Run(new myClass());
}
};
}
相关文章推荐
- C# 2.0 引入的匿名方法
- C#2.0_2-匿名方法
- C#2.0 锐利体验系列课程(2):匿名方法、迭代器 (Level 300)
- 第1节 C#2.0新特性之三:匿名方法
- C# 2.0:使用匿名方法、迭代程序和局部类来创建优雅的代码
- C# 2.0中的匿名方法
- 一起谈.NET技术,C#中的委托,匿名方法和Lambda表达式
- C# 2.0:使用匿名方法、迭代程序和局部类来创建优雅的代码
- C# 2.0 Specification(匿名方法)(二)
- C# 2.0 Specification(匿名方法)(二)
- C# 2.0 匿名方法、迭代器(选择自 shoutor 的 Blog )
- [读书笔记]C#学习笔记四: C#2.0泛型 可控类型 匿名方法和迭代器
- c#2.0新特性:(二)匿名方法(Anonymous Methods)
- C#积累(一)——扩展方法就近原则和匿名类型的成员探讨
- C# 2.0:使用匿名方法、迭代程序和局部类来创建优雅的代码
- C# 2.0 匿名方法与 Windows Forms 异步调用
- [读书笔记]C#学习笔记四: C#2.0泛型 可控类型 匿名方法和迭代器
- [读书笔记]C#学习笔记四: C#2.0泛型 可控类型 匿名方法和迭代器
- C#2.0 中的匿名方法
- C# 2.0 匿名方法简单解说