Question

Is there a way to declare a variable that has symbol involved? Such as:

int MIN% = 0;

or

string DE/M = "";
Was it helpful?

Solution

See Variable Naming Rules - C#

  • The first character of a variable name must be either a letter, an underscore character (_), or the at symbol (@).

  • Subsequent characters may be letters, underscore characters, or numbers.

You can't have any special character other than _ in variable name, (OR @ symbol as the first character) - So there is no point escaping it.

OTHER TIPS

C# is based on the Unicode character set. You can include certain (many, in fact) symbols in identifiers. For example, the following is valid code that will compile:

int myΔ = 1;
myΔ++;

int \u0066 = 1;
\u0066++;

As to the specific examples given (%, /), both are illegal characters in identifiers (most likely because they have other meaning within the language as operators).

This article as well as the C# specification provide additional information.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top