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 are static or fixed length arrays in C#?
A static array or fixed-length array in C# is a data structure with a predetermined size that cannot be changed after creation. Once you declare an array with a specific length, that size remains constant throughout the program's execution.
Static arrays are different from dynamic collections like List<T> because their size is immutable. This makes them memory-efficient and provides predictable performance characteristics.
Syntax
Following is the syntax for declaring a static array −
dataType[] arrayName = new dataType[size];
You can also initialize a static array with values at declaration −
dataType[] arrayName = {value1, value2, value3};
// or
dataType[] arrayName = new dataType[] {value1, value2, value3};
Creating Static Arrays
Example 1: Integer Array
using System;
class Program {
static void Main() {
// Create a static integer array with fixed size of 5
int[] numbers = new int[5] {10, 20, 30, 40, 50};
Console.WriteLine("Array elements:");
for (int i = 0; i < numbers.Length; i++) {
Console.WriteLine($"Index {i}: {numbers[i]}");
}
Console.WriteLine($"Array length: {numbers.Length}");
}
}
The output of the above code is −
Array elements: Index 0: 10 Index 1: 20 Index 2: 30 Index 3: 40 Index 4: 50 Array length: 5
Example 2: String Array with Static Members
using System;
public static class FruitStore {
static string[] _fruits = new string[] {
"apple",
"mango",
"orange",
"banana"
};
public static string[] Fruits {
get { return _fruits; }
}
}
class Demo {
static void Main() {
Console.WriteLine("Available fruits:");
foreach (string fruit in FruitStore.Fruits) {
Console.WriteLine("- " + fruit);
}
Console.WriteLine($"Total fruits: {FruitStore.Fruits.Length}");
}
}
The output of the above code is −
Available fruits: - apple - mango - orange - banana Total fruits: 4
Key Characteristics
Fixed Size: The array size is determined at creation and cannot be modified.
Contiguous Memory: Elements are stored in consecutive memory locations.
Zero-based Indexing: Array indices start from 0.
Type Safety: All elements must be of the same data type.
Default Values: Uninitialized elements get default values (0 for integers, null for objects).
Example 3: Demonstrating Fixed Size
using System;
class Program {
static void Main() {
// Create array with default values
int[] scores = new int[3];
Console.WriteLine("Default values:");
for (int i = 0; i < scores.Length; i++) {
Console.WriteLine($"scores[{i}] = {scores[i]}");
}
// Assign values
scores[0] = 95;
scores[1] = 87;
scores[2] = 92;
Console.WriteLine("\nAfter assignment:");
for (int i = 0; i < scores.Length; i++) {
Console.WriteLine($"scores[{i}] = {scores[i]}");
}
}
}
The output of the above code is −
Default values: scores[0] = 0 scores[1] = 0 scores[2] = 0 After assignment: scores[0] = 95 scores[1] = 87 scores[2] = 92
Static vs Dynamic Arrays
| Static Array | Dynamic Array (List<T>) |
|---|---|
| Fixed size at creation | Size can grow or shrink |
| Memory allocated once | Memory reallocated as needed |
| Better performance for known size | More flexible but slightly slower |
| Cannot add/remove elements | Can add/remove elements dynamically |
Conclusion
Static arrays in C# provide fixed-size collections with predictable memory usage and fast access times. They are ideal when you know the exact number of elements needed and want optimal performance for array operations.
