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
Declare a const array in C#
In C#, you cannot directly declare a const array because arrays are reference types and const fields must be compile-time constants. However, you can achieve similar behavior using readonly fields or ReadOnlyCollection to create immutable arrays.
Syntax
Following is the syntax for declaring a readonly array −
public static readonly DataType[] arrayName = { value1, value2, value3 };
Following is the syntax for using ReadOnlyCollection −
public static readonly ReadOnlyCollection<DataType> arrayName =
new ReadOnlyCollection<DataType>(new DataType[] { value1, value2, value3 });
Using readonly Static Arrays
The readonly keyword allows you to create an array that can only be assigned during declaration or in a constructor. Unlike const, readonly values can be set at runtime −
Example
using System;
public class VehicleTypes {
public static readonly string[] vehicles = { "Car", "Motorbike", "Cab" };
public static readonly int[] numbers = { 10, 20, 30, 40, 50 };
public static void Main() {
Console.WriteLine("Vehicle types:");
foreach (string vehicle in vehicles) {
Console.WriteLine(vehicle);
}
Console.WriteLine("\nNumbers:");
foreach (int number in numbers) {
Console.WriteLine(number);
}
// This would cause compile error:
// vehicles = new string[] { "Bus" };
}
}
The output of the above code is −
Vehicle types: Car Motorbike Cab Numbers: 10 20 30 40 50
Using ReadOnlyCollection
For complete immutability, use ReadOnlyCollection which prevents both reassignment and modification of array elements −
Example with ReadOnlyCollection Property
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
public class ImmutableArrays {
public static ReadOnlyCollection<string> Colors { get; } =
new ReadOnlyCollection<string>(new string[] { "Red", "Green", "Blue" });
public static void Main() {
Console.WriteLine("Available colors:");
foreach (string color in Colors) {
Console.WriteLine(color);
}
Console.WriteLine("\nTotal colors: " + Colors.Count);
// This would cause compile error:
// Colors[0] = "Yellow";
}
}
The output of the above code is −
Available colors: Red Green Blue Total colors: 3
Comparison of Approaches
| Approach | Reassignment Protection | Element Modification Protection | Performance |
|---|---|---|---|
| readonly array | Yes | No | Fast |
| ReadOnlyCollection | Yes | Yes | Slight overhead |
Runtime Assignment with readonly
Unlike const, readonly fields can be assigned values at runtime in constructors −
Example
using System;
public class DynamicArray {
public readonly string[] items;
public DynamicArray(int size) {
items = new string[size];
for (int i = 0; i < size; i++) {
items[i] = "Item " + (i + 1);
}
}
public static void Main() {
DynamicArray arr = new DynamicArray(3);
Console.WriteLine("Dynamic readonly array:");
foreach (string item in arr.items) {
Console.WriteLine(item);
}
}
}
The output of the above code is −
Dynamic readonly array: Item 1 Item 2 Item 3
Conclusion
While you cannot declare a true const array in C#, readonly arrays provide compile-time assignment protection, and ReadOnlyCollection offers complete immutability. Use readonly for performance-critical scenarios and ReadOnlyCollection when you need full protection against modifications.
