Understanding Java's “final” for translation to C#-Collection of common programming errors

The word you are looking for in C# is “const”:

const int SOMETHING = 10;

Note that the SIZE should be constant as well.

Also, constants can only be of type int, bool, string, char, etc (basic types only).

So if you want something else, such as an array or a class, you can do:

static readonly int[] PRED = new int[this.Nf];

Static Readonly means exactly const, logically, except it is defined a little differently backstage, which allows you to play more freely with it. (When you CAN – you SHOULD define constants instead of static readonly’s)

The array itself will not change during the run, so you will not be able to do this:

PRED = new int[3]; // Error
PRED = null; // Error
PRED = PRED; // Error

But you CAN change values INSIDE the PRED array:

PRED[0] = 123;

If you wish to have a readonly collection, you can use the ReadOnlyCollection object! If you also want that object to be constant, you can use the static-readonly combination (you can’t use constant because it is a class), and you will get:

static readonly ReadOnlyCollection PRED = new ReadOnlyCollection(new[] {1, 2, 5, 6});

And then PRED will always be PRED, and will always be of size 4, and will always contain 1, 2, 5, 6.

PRED = PRED; // Error (static readonly)
PRED = null; // Error (static readonly)
PRED[1] = 0; // Error (ReadOnlyCollection enforces readonly on the elements)

int i = PRED[1]; // Still works, since you aren't changing the collection.