C# Variables

Variables

Introduction to Variables in C#:

Variables in C# are containers for storing data values. They have a type that defines what kind of data they can hold and a name that identifies them within a program. C# is a statically typed language, meaning that you must declare the data type of each variable before you can use it.

1. Declaring Variables:

Variables in C# are declared using the following syntax:

int  age ;

Here, type is the data type (int) of the variable, and name  (age)is the identifier for the variable.

2. Declare Variable and Initialization Value:

Variables can also be declared and initialized in a single step:

Examples:

Scenario of Integer Variable:

1. In the First step Declare an integer variable named age then initialize the value in the second step.

// Declare an integer variable named ‘age’. It is currently uninitialized.

int age;

// Assign the value 25 to the variable ‘age’.

age = 25;

Integer Variable:

2. Declare and initialize an integer variable named age with the value 20 in a single step.

// Declare an integer variable named ‘age ’ and initialize it with the value 20.

int age = 20;

Course Video
Course Video In English
Frequently Asked Questions

Still have a question?

Let's talk

Variables in C# are named storage locations used to hold data during the execution of a program.

The main types of variables in C# are:

  • Local Variables: Declared inside a method and used within its scope.
  • Instance Variables: Non-static variables declared inside a class.
  • Static Variables: Declared with the static keyword, shared across all instances of a class.
  • Constant Variables: Values that don’t change, defined using the const keyword.

Variables in C# are declared using a data type followed by a variable name.
For example:
int number = 10;
string name = “John”;

 

You can learn variables in C# for free through online courses at Iqra Academy, which include examples, practical exercises, and video tutorials.

The syntax for variable declaration in C# is:
<data_type> <variable_name>  = <value>;
Example:
int age = 18;

Yes, beginner-friendly examples, like declaring integers, strings, or arrays, are included in free courses available at Iqra Academy.

A beginner tutorial covers:

  • Variable declaration and initialization.
  • Data types and scope.
  • Examples and best practices.

Variables are a core concept in programming, enabling you to store, manipulate, and retrieve data efficiently, which is crucial for building applications.

Absolutely! Iqra Academy offers free C# tutorials with Hindi videos, making it easier for beginners to learn variable concepts step by step.

Constants in C# are fixed values defined using the const keyword, while variables can hold data that can change during program execution.

Local variables are declared inside a method and are accessible only within that method. Instance variables are declared within a class and are accessible to all methods in the class.

Constant variables are those whose values cannot be changed once assigned. They are declared using the const keyword.
Example:
const double Pi = 3.14;


The var keyword allows the compiler to infer the variable’s data type, whereas explicit data types require you to specify the type.

Variable scope determines where a variable can be accessed in the program. It can be local (within a method), class-level, or global (static variables).

Uninitialized variables are automatically assigned default values, such as 0 for numeric types, false for booleans, and null for reference types.