Open In App

C# | Variables

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

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;

Initializing 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. Initialization can be done separately or may be with declaration.

Example :

int y = 7; // Declaring and initializing the variable at same time
int x; // Declaring variable x
x = 5; // initializing x with value 5

Two Ways for Initialization:

  1. Compile time initialization
  2. Run time initialization

1. 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 :




// 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);
    }
}


Output :

Value of x is 32
Value of y is 0

2. Run Time Initialization

In this, the user has to enter the value and that value is copied to the required variable. In this type of initialization, there is 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




// 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);
  
    }
}


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 the “num” variable. Hence the value will be displayed according to the user input.



Last Updated : 11 Feb, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads