bool Tubo(string text) { bool value; if (text == "Tubo") value = true; return value; }
Will this piece of code compile (answer without compiling please [:)])?
About .NET/C#, Xamarin, Flutter and other fun technologies
bool Tubo(string text) { bool value; if (text == "Tubo") value = true; return value; }
Will this piece of code compile (answer without compiling please [:)])?
You must be logged in to post a comment.
i’d have to say no.
since value isn’t allways assigned and it can be null when returning which is illegal.
haven’t tried it yet.
Should value assumed to be set as “false” by default ?
or… Will be a warning generated because value is not assigned ?
wait a minute… “value” is a reserved word used in get / set accessor:
I would say “fail to compile”. going to try it right now ๐
No?
Depends. It will generate warning but if you turn off that warning it will compile.
I agree with Mladen. value must always be initialized before used and that is not the case. BTW, value can’t be “null” because it is a value type. I’m sure Mladen just used “null” to describe “uninitialized”.
LaurentB: I think “value” is reserved only in property setter. It can be used in normal methods.
Hey guys,
The answer is no, it won’t compile because value variable isn’t defined in all case and thus compiler will yield an error.
And no, there is no problem in using “value” as a variable name outside settters (in fact I am using it often ๐ ).