In a computer language, a reserved word (also known as a reserved identifier) is a word that cannot be used as an identifier, such as the name of a variable, function, or label – it is "reserved from use".
C is a case sensitive language – which means a variable name declared as flag is not same as FLAG. They both will be treated as different variables. There are certain reserved words in C language, known as keywords. Words similar to a keyword can not be used as a variable name.
C Keywords Hence, keywords cannot be used as a variable name or a function name (in short as an Identifier) because that would try to change the existing meaning of the keyword, which is not allowed.
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.
As others have mentioned, you can escape a reserved word with @
.
In your example you don't really need to, I would write the property like this:
private string _long;
public string Long
{
get
{
return _long;
}
}
And the underscore and the capital L make it compile.
But it's kind of a tradition to call them Lat and Lon, or even better: Latitude and Longitude.
Yes, you can. Using the @ symbol.
This will work, for example: private string @long;
Doing this is highly not recommended, but it is possible.
Not an answer I know as I would steer clear of using reserved words as variable names, but if you insist then at least use the following:
private string lat;
private string @long;
public string Lat
{
get
{
return this.lat;
}
}
public string Long
{
get
{
return this.long;
}
}
I may be late to this party, but I thought I would throw in another place where using a reserved word as a variable name is a good idea!!
I am writing a web control, where I want one of the properties to be "class" in a similar manner as other elements have a "class" property.
So, indeed I will make my property be: "public string @class {get{} set{}}"
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