c# - CS0133 "The expression being assigned to 'identifier' must be constant" - what's the reason behind that? -


With many C ++ backgrounds I have been used to write the following:

 < Code> Const int count = ...; // Some non-trivial stuff here (int i = 0; i  i ++) {...}  

and I hope the same works fine in C # Will do However ...

  byte [] buffer = new byte [4]; Const int count = buffer.Length;  

produces error CS0133: the expression being assigned to 'count' should be continuous .

I do not get this. Why is it invalid? int is a value type, is not it? Why can not I specify a price and can not make variable changes in this way?

Because const in C ++ in const < / Code> is more const ;)

indicates continuous expression of compilation-time in C #, const . This will be similar to this C ++ code:

  enum {count = buffer.Length; }  

Because buffer. Length is evaluated on runtime, it is not a continuous expression, and therefore it will generate a compilation error.

There is a readable keyword in C # which is slightly more like C ++'s const . (It is still very limited, and there is no such thing as const-correctness in C #)


Comments

Popular posts from this blog

Eclipse CDT variable colors in editor -

AJAX doesn't send POST query -

wpf - Custom Message Box Advice -