C#风格:lambda表达式,_ =>或x =&GT ;? [英] C# style: Lambdas, _ => or x =>?

查看:142
本文介绍了C#风格:lambda表达式,_ =>或x =&GT ;?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用其他语言lambda表达式在C#中使用它们之前,所以我在习惯了使用的_ 在<形式简单的lambda表达式code> Func键< T,TResult> 特别是对于简单的lambda表达式在身体只是表示返回值的表达式(谓词等)。然而,在C#中我经常看到正在使用的,而不是单个字母 _ 这些同样类型的lambda表达式,如 X,Y,Z,I ,J,K 。这封信的做法似乎有些奇怪我,因为这些信件已经被普遍使用另一个典型意义,对循环变量。 _ 往往更容易阅读在我看来。 ?是单字母方式着实在C#lambda表达式建立的风格



例如:



我是什么用来写:

  VAR年龄= people.Select(_ => _.Age); 



我看到正在写什么,而不是:

  VAR年龄= people.Select(X => x.Age); //的'X'是不相符的
选择


解决方案

很多C#开发人员使用 _ 来表示该参数不打算使用,字母或其他短名称时,该参数的是正在使用的。



其他资源:




I've used lambda expressions in other languages before using them in C#, and so I got in the habit of using _ for simple lambdas in the form of Func<T, TResult>, especially for simple lambdas where the body is just an expression representing the return value (predicates, etc..). However, in C# I often see single letters being used instead of _ for these same sorts of lambda expressions, such as x,y,z,i,j,k. The letter approach seems odd to me since those letters already have another typical meaning in common usage, for loop variables. _ is often easier to read in my opinion. Is the single letter style really the established style for lambdas in C#?

Examples:

what I'm used to writing:

var ages = people.Select(_ => _.Age);

What I'm seeing being written instead:

var ages = people.Select(x => x.Age); // choice of 'x' is not consistent

解决方案

Many C# developers use _ to indicate that the parameter isn't going to be used and a letter or other short name when the parameter is being used.

Other resources:

这篇关于C#风格:lambda表达式,_ =&GT;或x =&GT ;?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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