What are static or fixed length arrays in C#?


A static array is a data structure with a fixed size. Let us see an example of a static array in C#.

Here is a static string array. The data remains the same here i.e. fixed −

static string[] _fruits = new string[] {
   "apple",
   "mango"
};

Now let us see the complete example to create and access static arrays in C# −

Example

using System;

class Demo {
   static void Main() {
      foreach (string fruits in Program.Fruits) {
         Console.WriteLine(fruits);
      }
   }
}

public static class Program {

   static string[] _fruits = new string[] {
      "apple",
      "mango"
   };

   public static string[] Fruits {
      get {
         return _fruits;
      }
   }
}

Samual Sam
Samual Sam

Learning faster. Every day.

Updated on: 21-Jun-2020

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements