您的位置:首页 > 编程语言 > C#

Part 98 Anonymous methods in c#

2015-09-12 02:53 615 查看
What is an anonymous method?

Anonymous method is a method without a name. Introduced in C# 2.0,they provide us a way of creating delegate instances without having to write a separate method.

public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
Button button = new Button();
button.Text = "click me";
this.Controls.Add(button);
//button.Click += button_Click;
button.Click += delegate(object send, EventArgs ea) {
MessageBox.Show("you click me by anonymous method");
};
}

void button_Click(object sender, EventArgs e)
{
MessageBox.Show("you just click me");
throw new NotImplementedException();
}
}


View Code
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: