Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

definition of static arrays

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?

like image 222
Simagen Avatar asked Jan 04 '11 14:01

Simagen


3 Answers

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];
like image 127
jjnguy Avatar answered Nov 08 '22 12:11

jjnguy


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

like image 41
MattDavey Avatar answered Nov 08 '22 11:11

MattDavey


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

     }
}
like image 2
Steven Avatar answered Nov 08 '22 12:11

Steven