Assigning default values to variables trivia

by Miha Markič 14. November 2007 16:57
bool Tubo(string text) { bool value; if (text == "Tubo") value = true; return value; }

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

Tags:

.net

Comments (6) -

Mladen
Mladen
11/14/2007 6:23:12 PM #

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.

Reply

LaurentB
LaurentB
11/14/2007 6:24:21 PM #

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 Laughing

Reply

Mike (the other Miha) :-)
Mike (the other Miha) :-)
11/14/2007 7:05:10 PM #

No?

Reply

Einar G.
Einar G.
11/15/2007 2:39:41 AM #

Depends. It will generate warning but if you turn off that warning it will compile.

Reply

Petar Repac
Petar Repac
11/15/2007 6:32:41 AM #

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.

Reply

Miha Markic
Miha Markic
11/15/2007 12:03:47 PM #

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

Reply

Add comment

  Country flag

biuquote
  • Comment
  • Preview
Loading

Miha Markic

About me
Righthand
 
Microsoft MVP
 
Developer Express' DXSquad
INETA Country Leader for Slovenia
INETA Country Leader for Slovenia

Slovene Developer Users Group Lead
Friends of Red-Gate
LLBLGenPro Partner

Miha currently works as a free lance consultant and software developer specialized in .net area.
He graduated in Computer and information science at the University of Ljubljana, Slovenia. He has accumulated experience in various programming languages such as Java, Visual Basic 3-6 (MCP), Visual C++, Delphi, C# and VB.Net through years.
He has experience in practically all (technical) stages of project development, including planning, framework development, user interface, business processes, as well as testing and documenting. He has worked on big and small projects in Slovenia and abroad (e.g. participated in completing level 3 IS for the Nucor steel plant, Hertford, USA).
Currently he enjoys programming in .net environment using C#. Since 2000 he has been active in Developer Express' DX Squad and has been ECDL trainer and tester. He also gives lectures on conferences and other events in Slovenia.

Month List

Tag cloud

Most comments

Paulius Paulius
1 comments
us United States
Meh Meh
1 comments
us United States
bart dm bart dm
1 comments
nl Netherlands

RecentComments

Comment RSS