definition of static arrays

35,072

Solution 1

When you talk about a 'static array' you are really talking about two separate things.

One would be the static keyword. When applied to variables this means that the variable lives at the class level, and each object of that type will not get its own instance.

An array is simply a data structure for holding multiple values of some type.

So, a static array is simply an array at the class level that can hold multiple of some data type.

For example:

In your TravelRoute class, you may have a set number of possible destinations in a route. These could be defined like so:

class TravelRoute {
    public static Destination[] possibleDestinations = 
           new Destination[]{
                 new Destination("New York"),
                 new Destination("Minneapolis"),
                 new Destination("NParis")
           };
}

This will define the possible destinations on a TravelRoute. You can then access the array like so:

Destination one = TravelRoute.possibleDestinations[0];

Solution 2

Do you possibly mean fixed size arrays?

unsafe struct Foo
{
    fixed int Values[8];
}

If so, you will get more search results by using fixed size arrays as your query :)

Solution 3

There's nothing (that I know of) that's special about static arrays, per se, which may be why you're having trouble finding good write-ups about them. Correct me if I'm wrong, but I wonder if it's the "static" part that you're most interested in? Basically, static means that the member exists at the class level rather than the instance level, so a static array would be an array that belongs to the class (rather than an instance of the class).

Example:

public class Foo
{
   public static int[] Numbers {get; set;}
}

public class Bar
{
   public int[] Numbers {get;set;}
}

public class Program
{
     public void Main()
     {
// NOTE: Foo itself has this array
          Foo.Numbers = new int[]{1,2,3};

// NOTE: it's this particular instance of a Bar that has this array
           Bar bar = new Bar();
           bar.Numbers = new int[]{1,2,3};

     }
}
Share:
35,072
Simagen
Author by

Simagen

Updated on July 09, 2022

Comments

  • Simagen
    Simagen almost 2 years

    I've been looking for a decent definition of static arrays. I've tried using msdn and c# sources but cannot seem to find a definition. It gives examples, but no definitions...

    Does anyone know any links or definitions and characteristics of a static array please?

  • Simagen
    Simagen over 13 years
    a fixed size array is what i eventually understood a static array to be, so thank you mattDavey for your input! it was much appeciated
  • Dean P
    Dean P over 2 years
    Any way to add a fixed size array as a member in a class?
  • MattDavey
    MattDavey over 2 years
    @DeanP No, "Fixed size buffer fields may only be members of structs". See: docs.microsoft.com/en-us/dotnet/csharp/misc/cs1642