C#中的“闭包”是什么? [英] What are 'closures' in C#?

查看:77
本文介绍了C#中的“闭包”是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

重复




.NET中的闭包


h2_lin>解决方案

C#中的关闭内联代表/ 匿名方法的形式a>。 闭包附加到其父方法,意味着父项方法体中定义的变量可以从匿名方法中引用。在这里有一个伟大的博客



示例



  public Person FindById (int id)
{
return this.Find(delegate(Person p)
{
return(p.Id == id);
}
}

您还可以查看 Martin Fowler Jon Skeet 博客。我相信你将能够从他们中的至少一个得到更多的深度细分。


Duplicate

Closures in .NET

What are closures in C#?

解决方案

A closure in C# takes the form of an in-line delegate/anonymous method. A closure is attached to its parent method meaning that variables defined in parent's method body can be referenced from within the anonymous method. There is a great Blog Post here about it.

Example

public Person FindById(int id)
{
    return this.Find(delegate(Person p)
    {
        return (p.Id == id);
    });
}

You could also take a look at Martin Fowler or Jon Skeet blogs. I am sure you will be able to get a more "In Depth" breakdown from at least one of them....

这篇关于C#中的“闭包”是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆