retide
retide

Reputation: 1190

What is difference between new in a constructor and new in a member declaration?

What is the difference between new in a constructor and new in a member declaration?

Example

public class PspGame {

  private List<string>name = new List<string>();
  private List<string>_value;

  public PspGame() {
    _value = new List<string>();
  }
}

What is the best way to do it and are there any performance issues?

Upvotes: 12

Views: 2118

Answers (5)

phoog
phoog

Reputation: 43076

Initializing fields in the constructor also allows you to avoid this problem:

class MyClass
{
    private List<string> _list = new List<string>();
    public MyClass()
    {
        //some logic here
    }
    public MyClass(List<string> initialList) : this()
    {
        _list = initialList;
    }
}

With this code, if you call the second constructor, you will needlessly create a list that is almost immediately abandoned and made eligible for garbage collection.

Upvotes: 3

Killnine
Killnine

Reputation: 5900

Having an argument in the constructor (non-default constructor), for me, allows for better testing. You can 'inject' certain data into a member field that you wouldn't necessarily be able to do without making that member public (or at least internal) or making a second call with a 'setter' property.

Because you can have multiple constructors, you could have a second one for testing, in conjunction with a default constructor, if you really wanted.

There aren't any real performance issues other than having to make a separate call to populate data later, or having less-maintainable code with multiple calls to the class (one to create the object, the second to populate the member).

EDIT: I realized I sorta answered the wrong question. I thought he was asking about the difference between default and non-default constructors. Now I see it was about a default constructor that initializes the member in the constructor vs. in member declaration...

Upvotes: 4

aroth
aroth

Reputation: 54854

They're pretty much equivalent (any differences in terms of performance and memory usage are negligible). The only real difference is that when you do:

private List<string>name = new List<string>();

...the assignment always happens no matter what constructor is used to create an instance of the object. When you do the assignment within a constructor, then it only happens when that specific constructor is used.

So if you have multiple constructors but you always want to initialize name exactly the same way, it is a bit shorter to use the first form than to explicitly initialize it in each constructor.

As a general rule, however, I prefer initializing fields in the constructor implementations, even if it does make the code more verbose in some cases.

Upvotes: 14

LeleDumbo
LeleDumbo

Reputation: 9340

Initialize instance members in constructor, initialize class members in declaration. AFAIK this is (only) the convention, and there's no performance penalty. IMHO this should be forced into language rule (syntax/semantic).

Upvotes: 1

TheEvilPenguin
TheEvilPenguin

Reputation: 5682

The two are equivalent. The compiler moves the initialization of any such members to the constructor (static constructor if the member is static). I wouldn't say there is a general best way to do it - just be consistent where possible, and if one makes more sense in the situation, use it.

Upvotes: 1

Related Questions