0
Default values of variables in Sololearn
I notice that a compile error (CS0165) occurs when I declare variables without assigning a value to them. Is it a limitation of Sololearn Code Playground? See the example: if I want to make a boolean variable true subject to such conditions I cannot write code like this: bool a; (...) a=true; Instead of this I have to write: bool a=false; (...) a=true;
3 Antworten
+ 4
It is not a 'limitation' of SoloLearn, it's a 'limitation' of C#.
Local variables must be initialized before they can be read.
There's some excellent answers here on why this is:
https://stackoverflow.com/questions/30816496/why-do-local-variables-require-initialization-but-fields-do-not
A Short answer: Makes things easier to see (bug free).
0
Edit: This is not a problem in case of reference types (I have checked string). Therefore it is possible that Playground requires initialization only for primitive types.
0
Thank you! There is a very comprehensive answer in that thread in Stacji Overflow.