definition of static arrays
Asked Answered
F

3

9

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?

Ferriage answered 4/1, 2011 at 14:49 Comment(1)
Maybe you are looking for a definition of "static"? msdn.microsoft.com/en-us/library/79b3xss3.aspxOenomel
A
14

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];
Airlia answered 4/1, 2011 at 14:52 Comment(0)
D
10

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 :)

Dnepropetrovsk answered 4/1, 2011 at 15:31 Comment(3)
a fixed size array is what i eventually understood a static array to be, so thank you mattDavey for your input! it was much appeciatedFerriage
Any way to add a fixed size array as a member in a class?Gapeworm
@DeanP No, "Fixed size buffer fields may only be members of structs". See: learn.microsoft.com/en-us/dotnet/csharp/misc/cs1642Dnepropetrovsk
H
2

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};

     }
}
Historicism answered 4/1, 2011 at 14:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.