什么是"=>" [英] what is '=>'
问题描述
"="的功能是什么? C#中的运算符.我在buddy.com
Whats is the functionality of "=>' operator in C#. I found this on some sample codes provided by buddy.com
AuthenticatedUser user;
client.CreateUserAsync((u, state) => {
user = u;
}, "username", "password");
推荐答案
The =>令牌称为lambda运算符.它在lambda表达式中用于将左侧的输入变量与右侧的lambda主体分开. Lambda表达式是类似于匿名方法的内联表达式,但更灵活.
它们广泛用于以方法语法表示的LINQ查询中.
下面的示例显示了两种查找和显示字符串数组中最短字符串长度的方法.示例的第一部分将lambda表达式(w => w.Length)应用于wordsarray的每个元素,然后使用Min方法
找到最小的长度.为了进行比较,该示例的第二部分展示了一个更长的解决方案,该解决方案使用查询语法执行相同的操作.
The => token is called the lambda operator. It is used in lambda expressions to separate the input variables on the left side from the lambda body on the right side. Lambda expressions are inline expressions similar to anonymous methods but more flexible;
they are used extensively in LINQ queries that are expressed in method syntax.
The following example shows two ways to find and display the length of the shortest string in an array of strings. The first part of the example applies a lambda expression (w => w.Length) to each element of the wordsarray and then uses the Min method to
find the smallest length. For comparison, the second part of the example shows a longer solution that uses query syntax to do the same thing.
-Alican
这篇关于什么是"=>"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!