[C# 5.0 IN A NUTSHELL,5th Edition读书笔记]Lambda表达式capture功能

Lambda表达式可以引用本地变量以及其所在的方法参数:
static void Main()
{
   int factor = 2;
   Func<int, int> multiplier = n => n * factor;
   Console.WriteLine (multiplier (3)); // 6
}


Lambda表达式引用的外部变量被称为被捕获的(captured)变量。捕获变量的Lambda表达式称为一个闭包(closure)。被捕获的变量只有在真正通过委托被调用的时候才会被赋值,而不是在其被捕获的时候。

int factor = 2;
Func<int, int> multiplier = n => n * factor;
factor = 10;
Console.WriteLine (multiplier (3)); // 30


Lambda表达式可以自己更新其捕获的变量:

int seed = 0;
Func<int> natural = () => seed++;
Console.WriteLine (natural()); // 0
Console.WriteLine (natural()); // 1
Console.WriteLine (seed); // 2



被捕获的变量的生命周期可以根据委托做相应的延长。下面的例子中,本地变量seed本应该在Natural方法执行完成后就消失掉。但因为它被捕获了,其生命周期也相应地随着捕获它的委托(natural)被延长了

static Func<int> Natural()
{
int seed = 0;
return () => seed++; // Returns a closure
}
static void Main()
{
Func<int> natural = Natural();
Console.WriteLine (natural()); // 0
Console.WriteLine (natural()); // 1
}



在Lambda表达式中初始化的本地变量每次委托调用都是独一无二的。如果我们重构之前例子中的代码,在lambda表达式中初始化seed变量, 我们就会得到一个不同的结果(在这个例子中,这个结果不是我们想要的):

static Func<int> Natural()
{
return() => { int seed = 0; return seed++; };
}
static void Main()
{
Func<int> natural = Natural();
Console.WriteLine (natural()); // 0
Console.WriteLine (natural()); // 0
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Book Description When you have a question about C# 5.0 or the .NET CLR, this bestselling guide has precisely the answers you need. Uniquely organized around concepts and use cases, this updated fifth edition features a reorganized section on concurrency, threading, and parallel programming—including in-depth coverage of C# 5.0’s new asynchronous functions. Shaped by more than 20 expert reviewers, including Microsoft’s Eric Lippert, Stephen Toub, Chris Burrows, and Jon Skeet, this book has all you need to stay on track with C# 5.0. It’s widely known as the definitive reference on the language. Get up to speed on C# language basics, including syntax, types, and variables Explore advanced topics such as unsafe code and type variance Dig deep into LINQ via three chapters dedicated to the topic Learn about code contracts, dynamic programming, and parallel programming Work with .NET features, including reflection, assemblies, memory management, security, I/O, XML, collections, networking, and native interoperability “C# 5.0 in a Nutshell is one of the few books I keep on my desk as a quick reference.” —Scott Guthrie, Microsoft “Whether you’re a novice programmer or an expert who wants to improve your knowledge of modern asynchronous programming techniques, this book has the information you need to get the job done in C#.” —Eric Lippert, Microsoft Table of Contents Chapter 1. Introducing C# and the .NET Framework Chapter 2. C# Language Basics Chapter 3. Creating Types in C# Chapter 4. Advanced C# Chapter 5. Framework Overview Chapter 6. Framework Fundamentals Chapter 7. Collections Chapter 8. LINQ Queries Chapter 9. LINQ Operators Chapter 10. LINQ to XML Chapter 11. Other XML Technologies Chapter 12. Disposal and Garbage Collection Chapter 13. Diagnostics and Code Contracts Chapter 14. Concurrency & Asynchrony Chapter 15. Streams and I/O Chapter 16. Networking Chapter 17. Serialization Chapter 18. Assemblies Chapter 19. Reflection and Metadata Chapter 20. Dynamic Programming Chapter 21. Security Chapter 22. Advanced Threading Chapter 23. Parallel Programming Chapter 24. Application Domains Chapter 25. Native and COM Interoperability Chapter 26. Regular Expressions

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值