Is there any benefit to doing the following:
public class Foo
{
private Bar bar;
public Foo()
{
bar = new Bar();
}
}
Instead of doing it like so:
public class Foo
{
private Bar bar = new Bar();
public Foo()
{
}
}
Given that at instantiation, the private member variable in either example will be instantiated, I don't believe there is a difference, but I've seen it enough times to where I'm curious.
In the exact case you've given, there isn't a difference - but in general there is.
Variable initializers are executed before the base class constructor is called. If that base constructor calls a virtual method which uses some of the instance variables, you can see that difference.
For spec fans, it's in section 10.11.2 of the C# 4 spec:
Here's an example demonstrating this:
Result:
Now having said the above, I would try hard to avoid calling virtual methods from a constructor. You're basically asking the derived class to work in a partially-initialized fashion. However, it's a difference you should be aware of.
As for where to initialize variables... I have to admit I'm not particularly consistent, and I don't find that's actually a problem. If I have any specific bias, it's probably to initialize anything which won't depend on any parameters at the point of declaration, leaving the variables which I can't initialize without extra information to the constructor.
In your case, there's no real difference in the functionality. There is the problem of figuring out where and how everything gets initialized, though. If i put the initialization in the constructor, i have two big benefits:
Everything's initialized in one place; i don't have to go hunting for whether and where it gets set.
If i want, i can pass arguments to the Bar constructor based on how i'm setting stuff up. If the initializer's outside the constructor, i'm a lot more limited in how i can initialize stuff.
Frankly, the IDE helps a bit with #1...though it does yank me away from the code i was just looking at, my classes are rarely so huge as to make re-finding stuff an issue. So if i don't need #2, i'm just as likely to do it either way depending on the project and my mood. For bigger projects, though, i'd want to have all my init code in one place.
Edit:
OK, apparently there is a difference between the two. But the case where it matters is rare.
I've created the following classes:
Now, what i found was a bit surprising, and unexpected (if you haven't read the C# spec).
This is what the
other
class's constructor compiled to:Note the call to Program::.ctor (the base class's constructor) sandwiched between the two
ldstr
/stfld
pairs (those are what sets1
ands2
). Meaning, when the base constructor is running,s2
has not been set yet.The program, for reference, outputs the following:
because in Program's constructor,
Console.WriteLine(obj)
calledobj.ToString()
, which (since the object is already another
) wasother::ToString()
. Sinces2
wasn't set yet, we didn't get a "World" the first time. If we were doing something more error prone than just printing stuff, this could cause real problems.Now, this is an ugly example, designed to be a pathological case. But it's an excellent argument against calling virtual functions in your constructor. Without doing just that, this breakage would not have been possible. That's the only time you really have to worry about the difference: when your base class's constructor is calling virtual methods that you've overridden, that rely on the values of any fields that are being set in the constructor. A pretty narrow window of breakability, but yeah.
Difference is in first case you have bar property initialized after constructor is called and in second before constructor is called. You don't use static methods so there is no difference.
A bit out of topic but best way is to initialise bar outside the object like this:
This way you are not coupling your objects.