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
Check if an Array has fixed size or not in C#
In C#, all arrays have a fixed size once they are created. You cannot change the length of an array after initialization. To check if an array has a fixed size, use the IsFixedSize property, which always returns true for arrays.
Syntax
Following is the syntax to check if an array has fixed size −
bool isFixed = arrayName.IsFixedSize;
Using IsFixedSize Property
Example
using System;
public class Demo {
public static void Main() {
string[] products = new string[] { "Electronics", "Accessories", "Clothing", "Toys", "Furniture" };
Console.WriteLine("Products list...");
foreach(string s in products) {
Console.WriteLine(s);
}
Console.WriteLine("Array Length: " + products.Length);
Console.WriteLine("Is the array having fixed size? = " + products.IsFixedSize);
Console.WriteLine("Is the array read-only? = " + products.IsReadOnly);
}
}
The output of the above code is −
Products list... Electronics Accessories Clothing Toys Furniture Array Length: 5 Is the array having fixed size? = True Is the array read-only? = False
Checking Empty Array Size
Example
using System;
public class Demo {
public static void Main() {
string[] emptyArray = new string[] { };
int[] numbers = new int[10];
Console.WriteLine("Empty array length: " + emptyArray.Length);
Console.WriteLine("Empty array has fixed size? = " + emptyArray.IsFixedSize);
Console.WriteLine("Numbers array length: " + numbers.Length);
Console.WriteLine("Numbers array has fixed size? = " + numbers.IsFixedSize);
}
}
The output of the above code is −
Empty array length: 0 Empty array has fixed size? = True Numbers array length: 10 Numbers array has fixed size? = True
Array vs Dynamic Collections
Unlike dynamic collections such as List<T>, arrays cannot grow or shrink after creation −
Example
using System;
using System.Collections.Generic;
public class Demo {
public static void Main() {
// Array - Fixed size
int[] fixedArray = new int[3] { 1, 2, 3 };
// List - Dynamic size
List<int> dynamicList = new List<int> { 1, 2, 3 };
Console.WriteLine("Array IsFixedSize: " + fixedArray.IsFixedSize);
Console.WriteLine("Array Length: " + fixedArray.Length);
Console.WriteLine("List Count before adding: " + dynamicList.Count);
dynamicList.Add(4);
Console.WriteLine("List Count after adding: " + dynamicList.Count);
}
}
The output of the above code is −
Array IsFixedSize: True Array Length: 3 List Count before adding: 3 List Count after adding: 4
Key Properties of Arrays
| Property | Description | Array Value |
|---|---|---|
IsFixedSize |
Indicates if the collection has a fixed size | Always true
|
IsReadOnly |
Indicates if the collection is read-only | Always false
|
Length |
Gets the total number of elements | Fixed at creation |
Conclusion
In C#, all arrays have a fixed size determined at creation time. The IsFixedSize property always returns true for arrays, indicating that you cannot add or remove elements after initialization. If you need a dynamically resizable collection, consider using List<T> or other collection classes instead.
