Default values of variables in Sololearn | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
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;

29th Aug 2017, 8:43 PM
Bartek
Bartek - avatar
3 Answers
+ 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).
30th Aug 2017, 1:14 AM
Rrestoring faith
Rrestoring faith - avatar
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.
29th Aug 2017, 8:53 PM
Bartek
Bartek - avatar
0
Thank you! There is a very comprehensive answer in that thread in Stacji Overflow.
30th Aug 2017, 5:56 AM
Bartek
Bartek - avatar