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.
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.
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.
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.
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.
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.
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)
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){...}
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With