Newbie question: How do I initialize an array

D

Dom

I need an array to be used by one method in a class. I can do this
three ways.

1. Put the array in the method. This seems like the best place,
since it is used only by the method. But it occurs to me that the
array must be initialized each time the method is called.

2. Put the array and the initialization in the class-level as a
static. (Why can't I make this a cons, BTW?). I don't like this
because I don't need it to be static or in the class-level.

3. Declare the array as a const at the class level, initialize it in
the constructor. This seems like the less-objectionable approach,
although I still have a class-wide array that is used only in a
method.

Which is the best approach, or is there something I've missed?

Dom
 
J

Jon Skeet [C# MVP]

Dom said:
I need an array to be used by one method in a class. I can do this
three ways.

1. Put the array in the method. This seems like the best place,
since it is used only by the method. But it occurs to me that the
array must be initialized each time the method is called.
Indeed.

2. Put the array and the initialization in the class-level as a
static. (Why can't I make this a cons, BTW?). I don't like this
because I don't need it to be static or in the class-level.

"const" in C# means "baked in at compile time" - only a few types can
be "const", and they don't include arrays. You can make it readonly,
although that only means the reference is readonly.
3. Declare the array as a const at the class level, initialize it in
the constructor. This seems like the less-objectionable approach,
although I still have a class-wide array that is used only in a
method.

Which is the best approach, or is there something I've missed?

I don't see why it's better to initialize it in a constructor if it's
constant - that means creating a new array for each instance of the
object, which is a waste of time if the contents won't change.

In terms of whether it's logical for it to be a part of the class or
not - classes are where state is stored, not methods. Methods only hold
"time-local" state, they don't logically get their own long-lived
state. There's nothing to stop you from only using a class member in
one particular method though, and that's fairly common. If you feel it
ought to be separated from the rest of the class, however, you could
move the method into its own class and have the state there.
 
D

Dom

Thanks for the response, and for the information about a const is.
Tell me something: are static variables in each instance of the
class, or are they represented only once, with each instance linking
to it?

And am I right that the following is optimal:

Class MyClass {
private static int [,] MyArray = { ... }

private int ToColumn () {
// uses MyArray
}

private int NextState () {
// uses ToColumn
}
}
 
J

Jon Skeet [C# MVP]

Dom said:
Thanks for the response, and for the information about a const is.
Tell me something: are static variables in each instance of the
class, or are they represented only once, with each instance linking
to it?

They're only represented once - it's not that they're shared between
instances, they're not specific to any instance at all. Basically
"static" = "belongs to the type rather than an instance of the type".
And am I right that the following is optimal:

Class MyClass {
private static int [,] MyArray = { ... }

private int ToColumn () {
// uses MyArray
}

private int NextState () {
// uses ToColumn
}
}

Yes, that looks right to me.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top