Part 98 Anonymous methods in c#
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.
class Program
{
static void Main(string[] args)
{
List<Person> persons = new List<Person>() {
new Person{ID=,Name="lin1"},
new Person{ID=,Name="lin2"},
new Person{ID=,Name="lin3"}
}; Person person = persons.Find(
delegate(Person p) //this is an anonymous method.
{
return p.ID == ;
}
);
Console.WriteLine("person id={0},name={1}", person.ID, person.Name); }
}
class Person
{
public int ID { get; set; }
public string Name { get; set; }
}
Another example:Subscribing for an event handler.
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();
}
}
Part 98 Anonymous methods in c#的更多相关文章
- Delegates, Events, and Anonymous Methods 委托、事件与匿名方法
http://www.cnblogs.com/r01cn/archive/2012/11/30/2795977.html
- 为什么说JAVA中要慎重使用继承 C# 语言历史版本特性(C# 1.0到C# 8.0汇总) SQL Server事务 事务日志 SQL Server 锁详解 软件架构之 23种设计模式 Oracle与Sqlserver:Order by NULL值介绍 asp.net MVC漏油配置总结
为什么说JAVA中要慎重使用继承 这篇文章的主题并非鼓励不使用继承,而是仅从使用继承带来的问题出发,讨论继承机制不太好的地方,从而在使用时慎重选择,避开可能遇到的坑. JAVA中使用到继承就会有两 ...
- 关于C#你应该知道的2000件事
原文 关于C#你应该知道的2000件事 下面列出了迄今为止你应该了解的关于C#博客的2000件事的所有帖子. 帖子总数= 1,219 大会 #11 -检查IL使用程序Ildasm.exe d #179 ...
- Visual Studio 2019 for Mac 离线更新方法
当你打开Visual Studio 2019 for Mac检查更新时,如果下载更新包很慢,可以尝试如下操作: 打开Finder(访达),找到~/Library/Caches/VisualStudio ...
- 20 行代码:Serverless 架构下用 Python 轻松搞定图像分类和预测
作者 | 江昱 前言 图像分类是人工智能领域的一个热门话题.通俗解释就是,根据各自在图像信息中所反映的不同特征,把不同类别的目标区分开来的图像处理方法. 它利用计算机对图像进行定量分析,把图像或图像中 ...
- C# in depth学习(1)
第一章,C#开发的进化史 1.简单数据类型 2.排序 Sorting an ArrayList using IComparer (C# 1) Sorting a List<Product> ...
- C#(转自wiki)
C#是微软推出的一种基于.NET框架的.面向对象的高级编程语言.C#的发音为"C sharp",模仿音乐上的音名"C♯"(C调升),是C语言的升级的意思.其正确 ...
- C#委托初探
委托是一种定义方法签名的类型,可以与具有兼容签名的任何方法关联.您可以通过委托调用其中已添加的方法列表.委托用于将方法作为参数传递给其他方法.事件处理程序就是通过委托调用的方法.您可以创建一个自定义方 ...
- Threading in C#
http://www.albahari.com/threading/ PART 1: GETTING STARTED Introduction and Concepts C# supports par ...
随机推荐
- Android studio启动后无法更新
I'm trying to run android-studio 1.0 on ubuntu using start up script but as i'm behind proxy the and ...
- __ARM_PROFILE_M__ __CORE__ __ARMVFP__ __LITTLE_ENDIAN__
__ARM_PROFILE_M__ Description An integer that is set based on the --cpu option. The symbol is set to ...
- CSS规则整理
一. 善用css缩写规则 /*注意上.右.下.左的书写顺序*/1. 关于边距(4边):1px 2px 3px 4px (上.右.下.左)1px 2px 3px (省略的左等于右)1px 2px (省略 ...
- HttpRequest类
一.HttpRequest的作用 HttpRequest的作用是令到Asp.net能够读取客户端发送HTTP值.比如表单.URL.Cookie传递过来的参数. 返回字符串的那些值就不说了,那些基本上都 ...
- 从零开始学android开发-项目打包发布
右键项目 选择[android tools]-[export signed application package] 点击[next] 如果没有keystore可以选择[create new keys ...
- C#操作XML的完整例子——XmlDocument篇
这是一个用c#控制台程序下, 用XmlDocument 进行XML操作的的例子,包含了查询.增加.修改.删除.保存的基本操作.较完整的描述了一个XML的整个操作流程.适合刚入门.net XML操作的 ...
- java 计算一个方法的返回执行时间
开始时间 long startTime = System.currentTimeMillis(); 程序业务逻辑代码() 结束时间 long endTime = System.currentTi ...
- 目标检測的图像特征提取之(一)HOG特征
1.HOG特征: 方向梯度直方图(Histogram of Oriented Gradient, HOG)特征是一种在计算机视觉和图像处理中用来进行物体检測的特征描写叙述子.它通过计算和统计图像局部区 ...
- 翻译学python---《Learn Python the hard Way》---第一章 绪论
打算学习python,但是又不想单纯地看书或是写个小项目,干脆引入很流行的翻译学习法来学习吧- 在论坛上看到了国外的一本<Learn Python the hard Way> ...
- Java Web系统经常使用的第三方接口
1. Web Service 接口 1.1 接口方式说明和长处 在笔者的开发生涯中,当作为接口提供商给第三方提供接口时,以及作为client去调用第三方提供的接口时,大部分时候都是使用 Web Se ...