Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does '@' char mean before parameter name in method declaration? [duplicate]

Tags:

c#

.net-3.5

Possible Duplicates:
What does the @ symbol before a variable name mean in C#?
What's the use/meaning of the @ character in variable names in C#?

Hi,

I have one quick question. What does '@' char mean before parameter name in method declaration? Like following:

protected void Method1(Type1 @arg1, Type2 arg2)
...

I use c# with .net 3.5.

Thanks.

like image 467
iburlakov Avatar asked Jan 19 '11 14:01

iburlakov


People also ask

What does '@' mean in C#?

In C#, a verbatim string is created using a special symbol @. @ is known as a verbatim identifier. If a string contains @ as a prefix followed by double quotes, then compiler identifies that string as a verbatim string and compile that string.

What is out_ in C#?

The out is a keyword in C# which is used for the passing the arguments to methods as a reference type. It is generally used when a method returns multiple values.

How to pass out parameter in C# function?

The out parameter in C# is used to pass arguments to methods by reference. It differs from the ref keyword in that it does not require parameter variables to be initialized before they are passed to a method. The out keyword must be explicitly declared in the method's definition​ as well as in the calling method.

Do you need to declare out variable before you use it?

Variables passed as out arguments do not have to be initialized before being passed in a method call. However, the called method is required to assign a value before the method returns.


4 Answers

It allows reserved words to be used as identifiers. It is usually used by code generators which may be using source names from systems with different keywords than the target language e.g. table names and sproc argument names.

like image 90
Tim Lloyd Avatar answered Nov 15 '22 17:11

Tim Lloyd


It is a way of escaping an identifier.

E.g. the following two are equivalent:

protected void Method1(Type1 @arg1, Type2 arg2)
protected void Method1(Type1 arg1, Type2 arg2)

@ is only really usefull if you need to name an identifier after a keyword. The below would not compile without the @:

protected void Method1(Type1 @class, Type2 arg2)

like image 31
Andrew Skirrow Avatar answered Nov 15 '22 18:11

Andrew Skirrow


For your specific example, it has no meaning. In C#, the @ symbol is used to escape keywords so that they can be used as variable names.

For instance, the following would generate a compiler error:

public void Test(string class){...}

But if you escape with @, it is fine:

public void Test(string @class){...}
like image 44
Brian Ball Avatar answered Nov 15 '22 17:11

Brian Ball


It is used to have reserved words as parameters. Example:

string @string = "abc";

or:

string @class = "foo";

arg1 is not a reserved word so using @ is not necessary. This being said, using reserved words to name your parameters is not a good idea. There are cases though where this is useful. For example in ASP.NET MVC some HTML extension methods take an anonymous object as parameter to emit html attributes. So you have syntax like this:

<%= Html.ActionLink("text", "action", "controller", null, new { @class = "abc" }) %>

which generates:

<a href="/controller/action" class="abc">text</a>
like image 25
Darin Dimitrov Avatar answered Nov 15 '22 19:11

Darin Dimitrov