It can be useful when reading some source code to know that after initialization, a variable will not change. For example in the following code sample:
for (int i = 0; i < Height; ++i)
{
for (int j = 0; j < Width; ++j)
{
int index = i * Width + j; // This variable never changes later. Or will it? [TrollFace]
float myValue = ComputeValue(i, j);
// ...
myArray[index] = myValue;
}
}
It would be nice for code reviewers to be able to tell them not to care too much about the index
variable because its value will never change. Obviously you can do it in a comment, but comments are not always updated and not necessarily reliable.
In some languages, a keyword can be used to ensure the assertion "the value of this local variable will not change after initialization". Is there such a keyword in C#?