Use the long reserved word as a variable name in C#
Yes, you can if you really want to:
private string @long;
The actual name of the variable (as reported by reflection etc) is just long
; the @
sign tells the compiler to ignore the fact that it's also a keyword.
I would very strongly advise against this, however.
How do I use a C# reserved keyword as a property name without the @ prefix?
You can't. It is a reserved keyword. That means "you can't". Contrast to "contextual keywords" which usually means "we added this later, so we needed it to work in some pre-existing scenarios".
The moderate answer here is: use @return
.
A better answer here is: rename your property. Perhaps ReturnValue
.
There is also the option of, say, Return
- but you might need to think about case-insensitive languages too.
Deserializing url-encoded-form-data with reserved word as variable name
The trick is to tell the compiler to not treat that name as the reserved keyword event
, and you do that with an at-sign, like this:
public string @event { get; set; }
Now, you might think this will be similar to using an underscore but that is not correct. The at-sign will actually not become part of the event name, it's just there to tell the compiler to not treat the next word as a reserved keyword, and instead treat it as an identifier.
Also note that everywhere you want to refer to this property, you likely have to keep using the at-sign prefix or that reference will not compile either.
Here's an example:
void Main()
{
Console.WriteLine(nameof(@event));
Console.WriteLine(nameof(_event));
}
public string @event { get; set; }
public string _event { get; set; }
Will output:
event
_event
C# keywords as a variable
string @string = "";
How do I use a C# keyword as a property name?
You can use keywords in C# as identifiers by prepending @ infront of them.
var @class = new object();
To quote from the MSDN Documentation on C# Keywords:
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.
Reserved words as names or identifiers
No, there is no way. That's why they're labeled "reserved".
How do I use a Javascript keyword as a variable name?
Well, if you insist, you can
var $break = 1;
which is the same as C# in terms of characters ;)
Seriously, you cannot use reserved keywords as variables, but they are allowed as property names:
myObj = {}
myObj.break = 'whatever';
Also, do note that the repertoire of the reserved words varies depending on the strictness. For example,
var interface = 1;
is valid in the non-strict mode, but breaks once you add use strict
to your script.
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)
Related Topics
How to Test If Type Is Primitive
Image Sequence to Video Stream
Use the Long Reserved Word as a Variable Name in C#
What's the Hardest or Most Misunderstood Aspect of Linq
Raise Event in High Resolution Interval/Timer
How to Get the Z-Order in Windows
SQL Error: Incorrect Syntax Near the Keyword 'User'
Best Way to Combine Two or More Byte Arrays in C#
Reflection - Getting the Generic Arguments from a System.Type Instance
Converting Xdocument to Xmldocument and Vice Versa
Divide Array into an Array of Subsequence Array
Constructor of an Abstract Class in C#
Difference Between Wiring Events with and Without "New"
What Are the Benefits to Marking a Field as 'Readonly' in C#
How to Upload Files in ASP.NET Core
How to Request Administrator Permissions When the Program Starts