Updated: Jan 19, 2022
A variable is the name given to a memory allocation that our programs can store and read data. Like other coding languages, each variable in C# has a unique type, which sets the boundaries and limitations of its allocated memory and the values that can be stored when declaring it in our program.
In C#, there is a range of variables we can use as part of our coding process, for example:
String – Stores text that is surrounded by double quotes “Text.”
Char – stores a single character surrounded by single quotes ‘char’.
Bool – Stores only two states of ‘true’ or ‘false.’
Int – keeps whole numbers, such as -10 or 10.
Double – stores numbers with decimals such as -10.7 or 10.7.
General rules for names for variables
You cannot use reserved words like (char or string) as names.
A Name of a variable must start with a letter.
Variables names are case sensitive (“MyName” and “Myname” are different variables).
Names can contain digits, underscore (_), and letters.
Names should start with a lowercase letter.
A variable name cannot contain whitespaces.
To create a variable, you must first specify the type and assign it a value (or initialize it in other parts of the code). Variables can be declared only in specific places in code, such as classes and methods, etc.
And here are some additional examples of other variables: