C# Variables (original) (raw)

Last Updated : 11 Jan, 2025

In **C#, variables are containers used to store data values during program execution. 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.

**In Brief Defination: 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.

**Syntax:

// Method 1
type variable_name = value;

// Method 2
type variable_names;

**Characteristics of Variables

To know, more about types/data-types refer to Data Types in C# article.

Rules for Naming Variables

**Examples:

**Valid Variables Names **Invalid Variables Names
int age; float _studentname; int if; float 12studentname;

Defining or Declaring a Variable

There are some rules that must be followed while declaring variables :

**Example:

int geeks;
float interest;

Before, checking how to declare and use variable we need to understand that every variable has a type/data-type defined. So, let us check what it actually means.

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:

// Declaring and initializing the variable at same time
int y = 7;

// Declaring variable x
int x;

// Initializing x with value 5
x = 5;

Two Ways for Variable Initialization

There are two basic ways for initialization as mentioned below:

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# `

// 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

Implementation:

C# `

// 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.