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
What is the difference between declaration and definition in C#?
In C#, declaration means specifying the type and name of a variable, method, or class without providing an initial value or implementation. Definition (or initialization) means providing an actual value or implementation to what was declared.
This distinction applies to variables, arrays, methods, and classes. Understanding the difference helps write cleaner code and avoid compilation errors.
Syntax
Following is the syntax for declaration −
datatype variableName;
Following is the syntax for definition/initialization −
variableName = value; // or combined declaration and initialization datatype variableName = value;
Variable Declaration vs Definition
Example
using System;
class Program {
static void Main() {
// Declaration only
int x;
string name;
// Definition/Initialization
x = 10;
name = "John";
// Combined declaration and definition
int y = 20;
string city = "New York";
Console.WriteLine("x = " + x);
Console.WriteLine("name = " + name);
Console.WriteLine("y = " + y);
Console.WriteLine("city = " + city);
}
}
The output of the above code is −
x = 10 name = John y = 20 city = New York
Array Declaration vs Definition
Example
using System;
class Program {
static void Main() {
// Declaration only
int[] numbers;
// Definition/Initialization
numbers = new int[3];
numbers[0] = 100;
numbers[1] = 200;
numbers[2] = 300;
// Combined declaration and definition
string[] names = {"Alice", "Bob", "Charlie"};
Console.WriteLine("Array elements:");
for(int i = 0; i < numbers.Length; i++) {
Console.WriteLine("numbers[" + i + "] = " + numbers[i]);
}
Console.WriteLine("Names array:");
for(int i = 0; i < names.Length; i++) {
Console.WriteLine("names[" + i + "] = " + names[i]);
}
}
}
The output of the above code is −
Array elements: numbers[0] = 100 numbers[1] = 200 numbers[2] = 300 Names array: names[0] = Alice names[1] = Bob names[2] = Charlie
Method Declaration vs Definition
In C#, methods are typically declared and defined together, but in interfaces and abstract classes, you can have method declarations without definitions −
Example
using System;
interface ICalculator {
// Method declaration only (no implementation)
int Add(int a, int b);
}
class Calculator : ICalculator {
// Method definition (implementation provided)
public int Add(int a, int b) {
return a + b;
}
}
class Program {
static void Main() {
Calculator calc = new Calculator();
int result = calc.Add(5, 3);
Console.WriteLine("Result: " + result);
}
}
The output of the above code is −
Result: 8
Key Differences
| Declaration | Definition |
|---|---|
| Specifies type and name | Provides actual value or implementation |
| Memory allocated with default values | Memory contains meaningful data |
| Can exist without definition | Requires prior declaration |
Example: int x;
|
Example: x = 10;
|
Conclusion
Declaration establishes the type and name of variables, methods, or classes, while definition provides the actual value or implementation. In C#, you can declare and define separately or combine both operations in a single statement for cleaner code.
