Tony said:
Family Tree Mike said:
Tony Johansson said:
[...]
My question is if it's any point to have like in my example this row
randomNumberGenerator = new Random();
in the c-tor
I mean it will be just the same if I remove the c-tor and change this row
private Random randomNumberGenerator;
to this row
private Random randomNumberGenerator = new Random(); [...]
My second question is if it's good programming to move statement from a
c-tor if I don't
pass any argument and place them as instance variable like I did above.
[...]
In this case, I don't believe it matters. In general, I would put the
initializations in a startup routine called by the constructor(s).
[...]
OK I can see the advantage of using the c-tor when an exception can be
thrown
_If_ you can do something useful with the exception and continue
initializing. In most cases, that won't be true.
As for the original question, normally it won't matter much. But IMHO
it's important to understand that the replies you've gotten aren't
literally correct. Specifically, member field initializers are in fact
handled differently from initialization in the constructor.
They aren't simply moved into the constructor. Rather, a more explicit
initialization is constructed from your declarations, that looks
something like the following. From something like this:
class A
{
public A()
{
// stuff
}
}
class B : A
{
private readonly int = 5;
public B()
{
// other stuff
}
}
…the class B construction winds up looking more like this:
ActualBConstruction()
{
ExecuteInitializers();
base(); // execute base constructor
this(); // execute this constructor
}
In other words, all of the member field initialization is done first,
from most-derived class to least, then followed by the execution of the
base class constructors, from least-derived class to most (i.e. in the
opposite order), finally followed by the actual constructor you wrote.
As I said, normally it doesn't matter much. You can initialize the
object either in the constructor or in the member field initializer
according to your own preference for how the code is laid out.
But if there are important side-effects to object construction for a
type, either because the constructor itself has side-effects, or because
it's the first time the type has been used and there's static
initialization in the class (which IMHO is basically always something to
consider as a side-effect), the order of those side-effects are not the
same as the order of the rest of the object constructor logic.
Finally, note my use of "readonly". Often when you are initializing a
member field like that, it's because the field will have the same value
throughout the lifetime of the class instance. Any time that's true,
you should go ahead and use "readonly" to make that explicit (you can
initialize "readonly" fields in a constructor…they just can't be changed
later).
Pete