Categories
.net

Assigning default values to variables trivia

bool Tubo(string text) { bool value; if (text == "Tubo") value = true; return value; }

Will this piece of code compile (answer without compiling please [:)])?

6 replies on “Assigning default values to variables trivia”

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 ๐Ÿ˜€

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 ๐Ÿ™‚ ).

Leave a Reply