What is a Variable?
A typical program uses various values that may change during its execution. For example, a program that performs some operations on the values entered by the user. The values entered by one user may differ from those entered by another user. Hence this makes it necessary to use variables as another user may not use the same values. When a user enters a new value that will be used in the process of operation, can store temporarily in the Random Access Memory of computer and these values in this part of memory vary throughout the execution and hence another term for this came which is known as Variables. So basically, a Variable is a placeholder of the information which can be changed at runtime. And variables allows to Retrieve and Manipulate the stored information.
type variable_name = value; or type variable_names;
char var = 'h'; // Declaring and Initializing character variable int a, b, c; // Declaring variables a, b and c of int type
Characteristics of Variables :
- name : It must be a valid identifier. In above example, var is a valid identifier.
- type : It defines the types of information which is to be stored into the variable. In above example char is a type.
- value : It is the actual data which is to be stored in the variable. In above example ‘h’ is the value.
Rules for Naming Variables
- Variable names can contain the letters ‘a-z’ or ’A-Z’ or digits 0-9 as well as the character ‘_’.
- The name of the variables cannot be started with a digit.
- The name of the variable cannot be any C# keyword say int, float, null, String etc.
Valid Variables Names
int age; float _studentname;
Invalid Variables Names
int if; // "if" is a keyword float 12studentname; // Cannot start with digit
Defining or Declaring a Variable
There are some rules that must be followed while declaring variables :
- specify its type (such as int)
- specify its name (such as age)
- Can provide initial value(such as 17)
int geeks; float interest;
The term initializing means to assign some value to the variable. Basically, the actual use of variables comes under the initialization part. In C# each data type has some default value which is used when there is no explicitly set value for a given variable. Intialization can be done seperately or may be with declaration.
int y = 7; // Declaring and intializing the variable at same time int x; // Declaring variable x x = 5; // intialzing x with value 5
Two Ways for Intialization:
- Compile time initialization
- Run time initialization
Compile Time Initialization
It means to provide the value to the variable during the compilation of the program. If the programmer didn’t provide any value then the compiler will provide some default value to the variables in some cases. Generally, this type of initialization helpful when the programmer wants to provide some default value.
Value of x is 32 Value of y is 0
Run Time Initialization
In this user has to enter the value and that value is copied to required variable. In this type of initialization there is a one more possibility in which value is assigned to variable after completion of a function call.
Input : 45 Output : Value of num is 45 Input : 27 Output : Value of num is 27
Value of num is 45
Note : Here the Console.ReadLine() method asks the user to enter the value and later on it puts the same value in “num” variable. Hence the value will be displayed according to the user input.
- SAS | How to specify a list of Variables?
- Scope of Variables in C#
- C# | Types of Variables
- JS++ | Variables and Data Types
- How to drop variables from a dataset in SAS Programming?
- C# | Implicitly Typed Local Variables - var
- How to avoid Compile Error while defining Variables
- Arrow operator -> in C/C++ with Examples
- Difference between Python and C#
- Difference between C and C#
- Writing to Excel Sheet Using EPPlus in C#
- 12 Reasons Why You Should Learn Python in 2019
- Lambda Expressions in C#
- How to Get Started with Game Development?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.