Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linq, lambda and @ [duplicate]

Possible Duplicate:
What's the use/meaning of the @ character in variable names in C#?
C# prefixing parameter names with @

I feel like I should know this and I find it impossible to google. Below is an excerpt from some Linq code I came across:

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();  

What does the '@' prefix mean in this context?

like image 757
Niklas Avatar asked Feb 01 '12 07:02

Niklas


1 Answers

Usually, the @ prefix is used if you need to use a keyword as an identifier, such as a variable name:

string @string = "...";

The C# spec says the following (emphasis mine):

The prefix "@" enables the use of keywords as identifiers, which is useful when interfacing with other programming languages. The character @ is not actually part of the identifier, so the identifier might be seen in other languages as a normal identifier, without the prefix. An identifier with an @ prefix is called a verbatim identifier. Use of the @ prefix for identifiers that are not keywords is permitted, but strongly discouraged as a matter of style.

Since t is not a keyword, I'd say that the @ should be removed in your example.

like image 161
Heinzi Avatar answered Sep 28 '22 15:09

Heinzi