【问题标题】:Linq, lambda and @ [duplicate]Linq、lambda 和 @ [重复]
【发布时间】:2012-02-01 07:52:39
【问题描述】:

可能重复:
What's the use/meaning of the @ character in variable names in C#?
C# prefixing parameter names with @

我觉得我应该知道这一点,但我发现用谷歌搜索是不可能的。 以下是我遇到的一些 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();  

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

【问题讨论】:

    标签: c# linq entity-framework lambda


    【解决方案1】:

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

    string @string = "...";
    

    C# spec 表示以下内容(强调我的):

    前缀“@”允许使用关键字作为标识符,这在与其他编程语言交互时很有用。字符 @ 实际上不是标识符的一部分,因此标识符在其他语言中可能被视为普通标识符,没有前缀。带有@ 前缀的标识符称为逐字标识符。 允许对不是关键字的标识符使用 @ 前缀,但出于风格考虑,强烈建议不要这样做。

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

    【讨论】:

    • 哈!我知道这是我应该知道的。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-05-25
    • 2016-07-26
    • 2011-05-29
    • 1970-01-01
    • 1970-01-01
    • 2012-12-07
    • 2011-09-05
    相关资源
    最近更新 更多