Variables in C#
what is a variable?
- A variable is simply a named storage location in your program's memory that holds a value.
- This value can be changed during program execution, making variables a powerful tool for manipulating data.
Declaring a Variable
- To use a variable in your program, you must first declare it. This means you need to specify its data type and give it a name.
- The data type defines the kind of data that the variable can hold, such as numbers or text. we will talk more about data types in the next chapter.
- For example, you can declare a variable that holds an integer value as:(C-1-1)
(C-1-1) |
- The above example creates a variable named myVariable that can hold integer values.
Note:
The semicolon at the end of the line is required to indicate the end of the statement.
Initializing a Variable
- You can also initialize a variable when you declare it. This means you give it an initial value that it held when it was first created.
- You can simply assign a value to the variable using the equal sign to do this.
- For example, to declare and initialize a variable named myVariable that holds the value 10, you would write:(C-1-2)
Using a Variable
- Once you have declared and initialized a variable, you can use it in your program.
- You can use the variable's name to refer to its current value or change its value by assigning a new value to it.
- For example, you can use a variable to add two numbers and store the result in a third variable as:(C-1-3)
(C-1-3) |
- In the above example, we create two variables named variable1 and variable2 that hold the values 10 and 15 respectively. Then we create a third variable named summation and assign it to the value of variable1 + variable2, which will be 25.
You can also display the summation on the console using WriteLine or write methods. (C-1-4)
(C-1-4) |
Rules for declaring variables in programming
- Variable names must begin with a letter or an underscore (_).
- Variable names cannot begin with a number.
- Variable names can only contain letters, numbers, and underscores(No special characters such as @,#,$,%,& except underscores).
- Variable names are case-sensitive.
- Variable names should be descriptive and meaningful.
- Variables must be declared with a data type.
- The data type of a variable determines the type of values it can hold.
- Variables can be initialized at the time of declaration, or later in the code.
- Multiple variables of the same data type can be declared on the same line, separated by commas.Example(C-1-5)
(C-1-5) |
Constants in C#
- A constant variable is a variable whose value cannot be changed once it has been assigned a value.
- It is declared using the const keyword followed by the data type and the variable name.
Example of how to declare a constant variable in C#:(C-1-6)
(C-1-6) |
- In the above example, we have declared a constant variable named myConstant of type int and assigned it a value of 10.
- Once the value of a constant variable is set, it cannot be changed during the execution of the program.
- Attempting to change the value of a constant variable will result in a compile-time error.
Conclusion
Variables are a fundamental concept in programming, and learning how to use them is an
essential part of becoming a proficient programmer. By understanding how to declare, initialize, and use variables, you can create powerful programs that manipulate data in useful ways.
Activity
Write a C# program that declares a variable named age and assigns it the value of your age.
Then, declare another variable as a name and assign it the value to your name. Finally, print the values of both variables to the console.
post your answer in the comment section.
إرسال تعليق