With a lot of C++ background I've got used to writing the following:
const int count = ...; //some non-trivial stuff here
for( int i = 0; i < count; i++ ) {
...
}
and I expected that the same would work fine in C#. However...
byte[] buffer = new byte[4];
const int count = buffer.Length;
produces error CS0133: The expression being assigned to 'count' must be constant.
I don't get it. Why is that invalid? int
is a value type, isn't it? Why can't I assign a value and make the variable unchangeable this way?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…