Variables are the fundamental of all programming languages. Variables
are used to store data such as strings of text, integers, and so on. The
variables' data or values can be set, modified, and retrieved as needed.
C# supports a wide range of different data types for variables.
- bool - stores a Boolean value (true or false)
- byte - stores an 8-bit unsigned integer
- short - stores a 16-bit signed integer
- int - stores a 32-bit signed integer
- long - stores a 64-bit signed integer
- float - stores a single-precision floating-point number
- double - stores a double-precision floating-point number
- decimal - stores a decimal
number with higher precision than a double
- char - stores a single Unicode character
- string - stores a sequence of Unicode characters
C# Variable Nameing Conventions
- Variable names are case-sensitive in C#. So, the variable names
msg, Myvar, myVar, MyVar are considered separate variables.
- Variable names can contain letters, digits, or the symbols underscore _
- A variable name cannot start with a digit 0-9.
- A variable name cannot be a reserved keyword in C# such as int or double
cannot be variable names.
To declare a variable in C#, you need to
specify its data type and give it a name.
string myTextname = "Rohatash";
int myNum = 5;
double myDoubleNum = 5.99D;
char myLetter = 'D';
bool myBool = true;
C# Variable with Constant Keyword
This will declare the variable as "constant", which means unchangeable and
read-only. The const keyword is useful when you want a variable to always store
the same value.
const int myNum = 30;
myNum = 15; // error
C# Multiple Variables
When you want to declare more than one variable of the same type, use a
int x = 3, y = 5, z = 70;
Console.WriteLine(x + y + z);