Welcome to RightHand's community place Sign in | | Help

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 Smile)?

Published 14. november 2007 15:57 by Miha Markic
Filed under:

Comments

# re: Assigning default values to variables trivia

14. november 2007 17:23 by Mladen
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.

# is value false by default ?

14. november 2007 17:24 by LaurentB
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 :D

# re: Assigning default values to variables trivia

14. november 2007 18:05 by Mike (the other Miha) :-)
No?

# re: Assigning default values to variables trivia

15. november 2007 1:39 by Einar G.
Depends. It will generate warning but if you turn off that warning it will compile.

# re: Assigning default values to variables trivia

15. november 2007 5:32 by Petar Repac
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.

# re: Assigning default values to variables trivia

15. november 2007 11:03 by Miha Markic

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 :-) ).

# Righthand blogs : How do you assign default values

PingBack from http://cs.rthand.com/blogs/blog_with_righthand/archive/2007/11/15/How-do-you-assign-default-values.aspx
Anonymous comments are disabled