C# | Variables

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.

Syntax :

type variable_name = value; 
or
type variable_names;

Example :

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.

Examples :

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)

Example :

int geeks;
float interest;

Intializing Variables

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.

Example :

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:

  1. Compile time initialization
  2. 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.

Example :

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to demonstrate the 
// Compile Time Initialization
using System;
class Geeks {
      
        // only declaration, compiler will 
        // provide the default value 0 to it
        int y;
      
    // Main Method
    public static void Main(String []args)
    {
          
        // Compile Time Initialization of variable 'x'
        // Assigning value 32 to x
        int x = 32;    
          
        // printing the value
        Console.WriteLine("Value of x is "+x);
          
        // creating object to access
        // the variable y
        Geeks gfg = new Geeks(); 
          
        // printing the value
        Console.WriteLine("Value of y is "+gfg.y);
    }
}

chevron_right


Output :

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.

Example :

Input : 45
Output : Value of num is 45

Input : 27
Output : Value of num is 27
filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to demonstrate the 
// Run Time Initialization
using System;
class Geeks {
      
    // Main Method
    public static void Main(String []args)
    {
          
        // Value will be taken from user 
        // input and assigned to variable
        // num
        int num = Convert.ToInt32(Console.ReadLine());
  
        // printing the result
        Console.WriteLine("Value of num is " + num);
  
    }
}

chevron_right


Output :

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.



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.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.




Article Tags :

1


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.