Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to declare variables in C#?
Each variable in C# has a specific type, which determines the size and layout of the variable's memory, the range of values that can be stored within that memory, and the set of operations that can be applied to the variable.
Variables must be declared before they can be used. Declaration tells the compiler what type of data the variable will hold and reserves memory accordingly.
Syntax
Following is the basic syntax for declaring variables in C# −
<data_type> <variable_name>;
You can also declare multiple variables of the same type in a single statement −
<data_type> <variable1>, <variable2>, <variable3>;
Variables can be initialized at the time of declaration −
<data_type> <variable_name> = <value>;
Common Data Types for Variable Declaration
C# supports various built-in data types. Here are some common examples −
int age; // Integer type float price; // Single-precision floating point double salary; // Double-precision floating point char grade; // Single character string name; // String of characters bool isActive; // Boolean (true/false)
Using Variable Declaration with Initialization
Example
using System;
class Demo {
static void Main() {
int x = 50;
float f = 3.14f;
double d = 99.99;
char grade = 'A';
string name = "John";
bool isStudent = true;
Console.WriteLine("Integer variable: " + x);
Console.WriteLine("Float variable: " + f);
Console.WriteLine("Double variable: " + d);
Console.WriteLine("Character variable: " + grade);
Console.WriteLine("String variable: " + name);
Console.WriteLine("Boolean variable: " + isStudent);
}
}
The output of the above code is −
Integer variable: 50 Float variable: 3.14 Double variable: 99.99 Character variable: A String variable: John Boolean variable: True
Using Multiple Variable Declaration
Example
using System;
class Demo {
static void Main() {
int a, b, c;
a = 10;
b = 20;
c = a + b;
Console.WriteLine("Value of a: " + a);
Console.WriteLine("Value of b: " + b);
Console.WriteLine("Value of c: " + c);
// Multiple declaration with initialization
int x = 100, y = 200, z = x * y;
Console.WriteLine("Product of x and y: " + z);
}
}
The output of the above code is −
Value of a: 10 Value of b: 20 Value of c: 30 Product of x and y: 20000
Variable Declaration Rules
-
Variable names must start with a letter or underscore (_).
-
Variable names cannot start with a digit.
-
Variable names are case-sensitive (age and Age are different variables).
-
Variable names cannot be C# keywords (like int, class, public, etc.).
-
Variables must be declared before they are used.
Conclusion
Variable declaration in C# requires specifying the data type followed by the variable name. Variables can be declared individually or in groups, and can be initialized at the time of declaration. Following proper naming conventions and understanding data types is essential for effective variable management in C# programming.
