什么是'=>'在C#中吗? [英] What does '=>' do in C#?

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

问题描述


可能显示的文件:结果
的Lamda解释和的结果
What是=> ?令牌称为






我看到这个代码:

  myContext.SomeEntities.Single(X => x.code ==代码); 

和我不知道什么是=>运营商做的。



有关运营商对谷歌的每个搜索到任何结果。



感谢您。


解决方案

=> 操作人员指定的 Lambda表达式




A lambda表达式是可以包含表达式和语句,可用于创建委托或表达式树类型的匿名函数。



所有的lambda表达式使用的lambda运算符=>,被读作变为。拉姆达操作者的左侧指定输入参数(如果有的话)和右侧保持的表达或语句块。 lambda表达式X => X * X读×去x次的X.这个表达式可以如下分配给委托类型:




 静态无效的主要(字符串[ ]参数)
{
Func键< INT,INT> FUNC = X => X * X;
INT J = FUNC(5);
∥焦== 25
}


Possible Duplicates:
Lamda Explanation and what it is as well as a good example
What is the => token called?

I have seen this code:

myContext.SomeEntities.Single(x => x.code == code);  

And I don´t know what does the => operator do.

Every search on google about the operator returns no results.

Thank you.

解决方案

The => operator designates a Lambda Expression:

A lambda expression is an anonymous function that can contain expressions and statements, and can be used to create delegates or expression tree types.

All lambda expressions use the lambda operator =>, which is read as "goes to". The left side of the lambda operator specifies the input parameters (if any) and the right side holds the expression or statement block. The lambda expression x => x * x is read "x goes to x times x." This expression can be assigned to a delegate type as follows:

static void Main(string[] args)
{
    Func<int, int> func = x => x * x;
    int j = func(5);
    // j == 25
}

这篇关于什么是'=&GT;'在C#中吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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