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
Does declaring an array create an array in C#?
Declaring an array in C# does not create the actual array object in memory. Array declaration only creates a reference variable that can point to an array object. The array must be explicitly initialized using the new keyword to allocate memory and create the array instance.
Array Declaration vs Array Creation
Understanding the difference between declaration and creation is crucial for working with arrays in C# −
Syntax
Following is the syntax for array declaration −
datatype[] arrayName;
Following is the syntax for array creation and initialization −
datatype[] arrayName = new datatype[size];
Declaration Without Creation
The following declaration creates only a reference variable, not an actual array −
int[] id; // Only declares a reference variable
At this point, id is null and attempting to access it would throw a NullReferenceException.
Array Creation with 'new' Keyword
The following creates an actual array object in memory −
int[] id = new int[5]; // Creates array with 5 elements
Example - Complete Array Operations
using System;
class ArrayDemo {
public static void Main(string[] args) {
// Declaration only - no array created yet
int[] numbers;
// Creation - memory allocated for 5 integers
numbers = new int[5];
// Initialize elements
for (int i = 0; i < 5; i++) {
numbers[i] = i + 10;
}
// Display array elements
Console.WriteLine("Array elements:");
for (int j = 0; j < 5; j++) {
Console.WriteLine("Element[{0}] = {1}", j, numbers[j]);
}
// Check array properties
Console.WriteLine("Array Length: " + numbers.Length);
}
}
The output of the above code is −
Array elements: Element[0] = 10 Element[1] = 11 Element[2] = 12 Element[3] = 13 Element[4] = 14 Array Length: 5
Different Ways to Create Arrays
Example - Multiple Creation Methods
using System;
class ArrayCreation {
public static void Main(string[] args) {
// Method 1: Declaration and creation separately
int[] arr1;
arr1 = new int[3];
// Method 2: Declaration and creation together
int[] arr2 = new int[3];
// Method 3: Creation with initialization
int[] arr3 = new int[3] {1, 2, 3};
// Method 4: Simplified initialization
int[] arr4 = {4, 5, 6};
Console.WriteLine("arr1 length: " + arr1.Length);
Console.WriteLine("arr2 length: " + arr2.Length);
Console.Write("arr3 elements: ");
foreach(int val in arr3) {
Console.Write(val + " ");
}
Console.WriteLine();
Console.Write("arr4 elements: ");
foreach(int val in arr4) {
Console.Write(val + " ");
}
Console.WriteLine();
}
}
The output of the above code is −
arr1 length: 3 arr2 length: 3 arr3 elements: 1 2 3 arr4 elements: 4 5 6
Conclusion
Array declaration in C# only creates a reference variable without allocating memory. The actual array object must be created using the new keyword, which allocates memory and initializes elements to their default values. Understanding this distinction helps avoid null reference errors when working with arrays.
