Linq, lambda and @
本文关键字:and lambda Linq | 更新日期: 2023-09-27 18:22:17
可能重复:
什么';C#中变量名中@字符的用法/含义是什么
C#在参数名称前面加上@
我觉得我应该知道这一点,但我发现谷歌是不可能的。以下是我遇到的一些Linq代码的摘录:
private static readonly Func<SomeEntities, someThing, List<someThing>> GetReplacement =
(someEntities, thing) => someEntities.someReplacement.Join(someEntities.someThing,
replaces => replaces.new_thing_id,
newThing => newThing.thing_id,
(rep, newThing) => new {rep, newThing}).Where(
@t => @t.rep.thing_id == thing.thing_id).
Select
(
@t => @t.newThing
).ToList().Distinct(new SomeThingComparer()).ToList();
"@"前缀在此上下文中是什么意思?
通常,如果需要使用关键字作为标识符,则会使用@
前缀,例如变量名:
string @string = "...";
C#规范说明了以下内容(重点是我的):
前缀"@"允许使用关键字作为标识符,这在与其他编程语言接口时很有用。字符@实际上不是标识符的一部分,因此在其他语言中,标识符可能被视为正常标识符,没有前缀。前缀为@的标识符称为逐字标识符允许为非关键字的标识符使用@前缀,但出于风格考虑,强烈建议不要使用@前缀
由于t
不是一个关键字,我认为在您的示例中应该删除@
。