Modificador C # @ para parámetros de métodos

I was using ReSharper plugin on VS2010 and i was generating an interface method. ReSharper put an @ on the parameter name. WHat is that used for?

int Count(Func<ContratoList, bool> @where);

Whats the difference for

int Count(Func<ContratoList, bool> where);

¡Gracias!

preguntado el 08 de noviembre de 11 a las 13:11

Look at this posting about the @ verbatim operator sanity-free.org/112/… -

6 Respuestas

El @ symbol allows you to use palabras reservadas en un nombre de variable.

int @class = 1;

void MyMethod(int @goto);

bool @public { get; set; }

As Marc correctly pointed out in his comment and his answer, ReSharper is actually wrong to do this because where es una Contextual Keyword and is not actually a reserved word, so your method will compile without the @.

respondido 08 nov., 11:20

where is explicitly not a reservado keyword; it is a contextual keyword. This means that in effect, the @where in the question achieves: exactly nothing. - Marc Gravell

Yeah, you're right. It does say that in the link I included in the answer, but I assumed it to be the same with where because of the question. I've upvoted your answer though, well spotted! - Connell

Resharper is overzealous with adding @ to things. - Ritch Melton

In many ways, resharper is wrong to do this. where es una contextual keyword, meaning: it only acts as a keyword in some very specific scenarios (i.e. LINQ). In the position indicated, it actually does nothing. It would not be confused as a keyword there, as when the C# language designers add keywords to C# they need to ensure pre-existing code continues to compile (as far as possible), and that would have been legal in earlier C#.

El uso de @ also confuses/complicates some tools (razor, in particular, since razor already uses @ to indicate the start of code - meaning that for a variable using @ (es decir @string) sometimes you need @ and sometimes you need @@ - and I know of at least one false-positive IDE warning this can cause).

However! If the parameter was if or class etc, then @if / @class allows you to use that as a variable name rather than getting confused as a C# keyword. That Además, isn't a great idea, note. But by the same token, we wouldn't start doing that to todos our code (string @name = ... etc) - so why do it here? It isn't needed, and as demonstrated by this question, it has added confusion.

Personally, though, I'd find a parameter name that isn't a keyword or contextual-keyword.

respondido 08 nov., 11:18

It may also be worth adding that where is not only a LINQ keyword, but also used for generic type constraints - Connell

Yes, designers are trying to ensure that existing code doesn't break as far as possible. However, I was quite surprised when one Monday morning the module that no one touched for a couple of months suddenly refused to build. Turns out, it used async as a method parameter name. Morale: don't install Async CTP before going on week end - Dyppl

It allows you to use reserved keywords as variable names.

respondido 08 nov., 11:17

It will stop this parameter acting like a keyword (as whereis a keyword in linq amongst other things).

respondido 08 nov., 11:17

It would not act as a keyword in that position. - Marc Gravell

@Marc - that's a good point. It must just be applying it as a general rule - trying to do it perfectly by context would be more of a challenge I guess. - Jon Egerton

This is done because 'where' is a protected keyword in C# (LINQ)

respondido 08 nov., 11:17

it allows you to use reserved words as params.

respondido 08 nov., 11:17

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.