Linq,lambda和@

Nik*_*las 5 c# linq lambda entity-framework

可能重复:
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();  
Run Code Online (Sandbox Code Playgroud)

在这种情况下,'@'前缀是什么意思?

Hei*_*nzi 14

通常,@如果您需要使用关键字作为标识符(例如变量名称),则使用前缀:

string @string = "...";
Run Code Online (Sandbox Code Playgroud)

C#规范说以下(重点煤矿):

前缀"@"允许使用关键字作为标识符,这在与其他编程语言交互时很有用.字符@实际上不是标识符的一部分,因此标识符可能在其他语言中看作普通标识符,没有前缀.带有@前缀的标识符称为逐字标识符.允许使用@前缀作为非关键字的标识符,但强烈建议不要使用样式.

由于t不是关键字,我会说@应该在您的示例中删除.