How do you assign default values

In previous post I was asking whether a method returning an unassigned variable will compile and the answer is no, it won’t. So this brings an interesting question, how do you initialize default values?

Are you doing something like this (1) – assigning default value right at the start:

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

or this (2):

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

or this one (3) (valid only for bool results):

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

Personally I would go with the last one if I am dealing with bool values. If not, I prefer the first one.

5 replies on “How do you assign default values”

I usually don’t set default values inside such methods, just to avoid double assigments (value = false, then value = true). So I guess it’s #2 for me (with #3 for bools that is). I also tend to use guard statements like [and this is just a simplified example!]:

bool Tubo(string text)
if (text == “Tubo”)
return true;

return false;

Leave a Reply