In C#, variables are essential elements used to store and manage data. They allow you to store various types of information, such as numbers, text, and boolean values.
An integer variable is used to store whole numbers without decimal points. It's represented by the int data type. Let's declare and initialize an integer variable age with the value 25:
int age = 25;
In the above example:
int specifies the data type as an integer.
age is the variable's name.
= is the assignment operator used to assign a value.
25 is the assigned value.
String variables are used to store sequences of characters, such as text. They're represented by the string data type. Let's declare and initialize a string variable name with the value "John":
string name = "John";
Here's what each part means:
string signifies the data type as a string.
name is the name of the variable.
= is the assignment operator.
"John" is the assigned value, enclosed in double quotes.
Double variables are used to store numbers with decimal points. They're represented by the double data type. Let's declare and initialize a double variable salary with the value 50000.75:
double salary = 50000.75;
In the above example,
double indicates the data type as a double-precision floating-point number.
salary is the variable's name.
= is the assignment operator.
50000.75 is the assigned value, which includes the decimal point.
Boolean variables are used to represent true or false conditions. They're represented by the bool data type. Let's declare and initialize a boolean variable isStudent with the value true:
bool isStudent = true;