What Does Placing a @ in Front of a C# Variable Name Do

What does placing a @ in front of a C# variable name do?

It's just a way to allow declaring reserved keywords as vars.

void Foo(int @string)

What is @ in front of a variable / identifier in C#?

In this case it's completely unnecessary, but it allows you to use any keyword as an identifier in C#. It doesn't change the meaning of the identifier at all, or how it's used - it only tells the compiler that you don't want the following characters to be recognized as a keyword.

For example:

string @int = "hello";
var @void = @int;

Using it for an identifier of claimsList suggests that whoever wrote it doesn't understand it. The fact that the identifier is for a string variable is entirely irrelevant here.

Personally I've pretty much only ever used the feature for extension methods, where I have been known to call the first parameter @this:

public static void Foo(this Bar @this)
{
return @this.Baz() * 2;
}

What's the use/meaning of the @ character in variable names in C#?

Straight from the C# Language Specification, Identifiers (C#)
:

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.

What does the @ symbol before a variable name mean in C#?

The @ symbol allows you to use reserved word. For example:

int @class = 15;

The above works, when the below wouldn't:

int class = 15;

What does variable names beginning with _ mean?

There's no language-defined meaning - it's just a convention some people use to distinguish instance variables from local variables. Other variations include m_foo (and s_foo or g_foo or static variables) or mFoo; alternatively some people like to prefix the local variables (and parameters) instead of the instance variables.

Personally I don't use prefixes like this, but it's a style choice. So long as everyone working on the same project is consistent, it's usually not much of an issue. I've seen some horribly inconsistent code though...

C# @ symbol in variable names

class is a reserved word in C# to denote a new type. You can't have a variable name that is a reserved word, so you use @ to 'escape' the symbol.

AKA:

int int = 4; // Invalid
int @int = 4; // Valid

What is the purpose of @ as part of a member name in C#?

You are correct. See C# Keywords, section 2.4.2 Identifiers of the language specification, or string (C# Reference).

From the keywords topic:

Keywords are predefined, reserved
identifiers that have special meanings
to the compiler. They cannot be used
as identifiers in your program unless
they include @ as a prefix. For
example, @if is a valid identifier but
if is not because if is a keyword.

Why do we use _ in variable names?

It doesn't mean anything. It is rather a common naming convention for private member variables to keep them separated from methods and public properties. For example:

class Foo
{
private int _counter;

public int GetCounter()
{
return _counter;
}

public int SetCounter(int counter)
{
_counter = counter;
}
}

What does '@' do in c#?

In this case its a valid name. It is used if you want to use a keyword as a variable name like @class.

See: What does placing a @ in front of a C# variable name do?



Related Topics



Leave a reply



Submit