0

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#?

wip
  • 2,313
  • 5
  • 31
  • 47
  • 1
    what about `readonly`? – Olivarsham Feb 26 '16 at 04:08
  • You mean like `static` in C? – Ian Feb 26 '16 at 04:12
  • @Olivarsham `readonly` only use for global variables – NoName Feb 26 '16 at 04:12
  • @Ian No, I mean like `const`. – wip Feb 26 '16 at 04:12
  • @wil the value of `index` variable will change definitely. Because it depends on other variables such as `width`, `i`, and `j` and the value for those variables are changing. – Nishat Lakhani Feb 26 '16 at 04:21
  • @NishatLakhani Not true. For each locally scoped instance of `index` the value remains constant. Each iteration has a new `index`, which does have a different value each time however. – lc. Feb 26 '16 at 04:22
  • @lc I am also saying the same thing that index value after every iteration will change. – Nishat Lakhani Feb 26 '16 at 04:25
  • @NishatLakhani Right, but the value does not change for each instance of `index`. The OP is looking for a way to assert this to the compiler, so doing `index = 0x2bad2d1e` later on throws an error. – lc. Feb 26 '16 at 04:29
  • I voted this question down because why would any code review be concerned for a left side assignment if the right side is correct? The value of INDEX changes on every iteration most definitely. The question "This value never changes later" is ambiguous. Later than what? The next iteration? or the current iteration? – JWP Feb 26 '16 at 04:43
  • 1
    @JohnPeters: Variable lifetime is really not such a difficult concept, and it's important to understand for other purposes (such as whether a lambda capture inside the loop accesses the same variable, or a new one for each instance) – Ben Voigt Feb 26 '16 at 04:46
  • The question is not using lambdas this is a question that shows zero understanding of loops. As mentioned, each time that line is executed it's value changes. The array is set which is outside the scope of the loop. Fundamental basic C# – JWP Feb 26 '16 at 04:51
  • If user wanted to set index to a getter setter property outside the loop he could use private set, but why? – JWP Feb 26 '16 at 04:52

1 Answers1

4

As far as I know, there is no such feature. There is the readonly keyword, but this is for class fields, not local variables.

The only solution I can come up with is to create a wrapper class, which initializes a get-only property. For ease of use, you can add implicit cast operators to and from the underlying type. But there is definitely some performance overhead to this, and it's probably not worth it.

Interestingly enough, there seems to be a question already discussing why there is no such feature.

Community
  • 1
  • 1
lc.
  • 113,939
  • 20
  • 158
  • 187