What Does the @ Symbol Before a Variable Name Mean in C#

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'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.


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 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 does '@' char mean before parameter name in method declaration?

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.

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...

What is the @ Sign in front of parameters

An @ sign lets you use C# keywords in identifiers; if you remove the @ then you'll get a syntax error.

Prolog: what does a 's' before a variable name stand for?

By itself s (or any other functor, for that matter) do not have any intrinsic meaning.

It gets meaning by the way it is utilized in the Prolog code (and queries).


Telling from the MASTERMIND.PL source code that your question links to, the unary prefix operator s/1 gets utilized in
extend_code/1 (line #110), finished/1 (line #173), determine_blacks/5 (line #231), and determine_whites/5 (line #247).

s/1 is defined as a unary prefix operator1 in line #54:

:- op(150, fy, s).

All this does is allow us writing s s s 0 instead of s(s(s(0))).

In my opinion, this provides no benefit whatsoever.


Footnote 1: For more information on Prolog operators, read this part of the SICStus Prolog manual.

what does symbol '?' after type name mean

structs (like int, long, etc) cannot accept null by default. So, .NET provides a generic struct named Nullable<T> that the T type-param can be from any other structs.

public struct Nullable<T> where T : struct {}

It provides a bool HasValue property that indicates whether the current Nullable<T> object has a value; and a T Value property that gets the value of the current Nullable<T> value (if HasValue == true, otherwise it will throw an InvalidOperationException):

public struct Nullable<T> where T : struct {
public bool HasValue {
get { /* true if has a value, otherwise false */ }
}
public T Value {
get {
if(!HasValue)
throw new InvalidOperationException();
return /* returns the value */
}
}
}

And finally, in answer of your question, TypeName? is a shortcut of Nullable<TypeName>.

int? --> Nullable<int>
long? --> Nullable<long>
bool? --> Nullable<bool>
// and so on

and in usage:

int a = null; // exception. structs -value types- cannot be null
int? a = null; // no problem

For example, we have a Table class that generates HTML <table> tag in a method named Write. See:

public class Table {

private readonly int? _width;

public Table() {
_width = null;
// actually, we don't need to set _width to null
// but to learning purposes we did.
}

public Table(int width) {
_width = width;
}

public void Write(OurSampleHtmlWriter writer) {
writer.Write("<table");
// We have to check if our Nullable<T> variable has value, before using it:
if(_width.HasValue)
// if _width has value, we'll write it as a html attribute in table tag
writer.WriteFormat(" style=\"width: {0}px;\">");
else
// otherwise, we just close the table tag
writer.Write(">");
writer.Write("</table>");
}
}

Usage of the above class -just as an example- is something like these:

var output = new OurSampleHtmlWriter(); // this is NOT a real class, just an example

var table1 = new Table();
table1.Write(output);

var table2 = new Table(500);
table2.Write(output);

And we will have:

// output1: <table></table>
// output2: <table style="width: 500px;"></table>


Related Topics



Leave a reply



Submit