C# | Check if an Array has fixed size or not
Array.IsFixedSize Property is used to get a get a value indicating whether the Array has a fixed size. This property implements the IList.IsFixedSize
Property .
Syntax:
public bool IsFixedSize { get; }
Property Value: This property is always return true for all arrays.
Below programs illustrate the use of above-discussed property:
Example 1:
// C# program to illustrate // IsFixedSize of Array class using System; namespace geeksforgeeks { class GFG { // Main Method public static void Main() { // declares an 1D Array of string string [] topic; // allocating memory for topic. topic = new string [] { "Array, " , "String, " , "Stack, " , "Queue, " , "Exception, " , "Operators" }; // Displaying Elements of the array Console.WriteLine( "Topic of C#:" ); foreach ( string ele in topic) Console.WriteLine(ele + " " ); Console.WriteLine(); // Here we check whether is // array of fixed size or not Console.WriteLine( "Result: " + topic.IsFixedSize); } } } |
Output:
Topic of C#: Array, String, Stack, Queue, Exception, Operators Result: True
Example 2:
// C# program to illustrate // IsFixedSize of Array class using System; namespace geeksforgeeks { class GFG { // Main Method public static void Main() { // declares a 1D Array of string // and assigning null to it string [] str = new string [] { null }; // Here we check whether is // array of fixed size or not Console.WriteLine( "Result: " + str.IsFixedSize); } } } |
Output:
Result: True
Note:
- Array implements the IsFixedSize property because it is required by the
System.Collections.IList
interface. - An array with a fixed size does not allow the addition or removal of elements after the creation of an array, but modification of existing elements are allowed.
- Retrieving the value of this property is an O(1) operation.
Reference:
Please Login to comment...