Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# @ modifier for methods parameters

Tags:

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

Thanks!

like image 959
renanleandrof Avatar asked Nov 08 '11 13:11

renanleandrof


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is C in C language?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.

Is C language easy?

Compared to other languages—like Java, PHP, or C#—C is a relatively simple language to learn for anyone just starting to learn computer programming because of its limited number of keywords.

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.


2 Answers

The @ symbol allows you to use reserved words in a variable name.

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 is a Contextual Keyword and is not actually a reserved word, so your method will compile without the @.

like image 200
Connell Avatar answered Oct 17 '22 01:10

Connell


In many ways, resharper is wrong to do this. where is a 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#.

The usage of @ also confuses/complicates some tools (razor, in particular, since razor already uses @ to indicate the start of code - meaning that for a variable using @ (i.e. @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 also isn't a great idea, note. But by the same token, we wouldn't start doing that to all 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.

like image 43
Marc Gravell Avatar answered Oct 17 '22 02:10

Marc Gravell