C# variables
In C#, a variable is a named storage location that holds a value of a particular data type. Variables are used to store and manipulate data in the code.
To declare a variable in C#, you use the following syntax:
data_type variable_name;
Here, data_type
specifies the type of data that the variable can hold (e.g. int
, double
, string
, bool
, etc.), and variable_name
is the name of the variable.
Here is an example of declaring and initializing variables in C#:
int age; // declaration age = 30; // initialization double salary = 50000.50; // declaration and initialization string name = "John"; // declaration and initialization bool isEmployed = true; // declaration and initialization
You can also declare and initialize variables in the same statement, like this:
int x = 10; double y = 20.5; string message = "Hello, world!";
Once a variable is declared and initialized, you can use it in expressions and assign new values to it. For example:
int x = 10; int y = 5; int sum = x + y; // sum is 15 x = 20; // x is now 20
Note that variables in C# are case-sensitive, so age
, Age
, and AGE
are considered three different variables. It's also important to choose meaningful variable names that describe the data they hold.