Mason11987
Mason11987

Reputation: 330

What's the best way to handle List or Dictionaries within Objects in C#

I often create objects which contain lists or dictionaries

What's the ideal way of handling this?

For Example:

class Person
{
    public List<person> Friends { get; set; }
}

But this means I need to verify that Friends is initialized prior to adding items to it. So I sometimes go with:

class Person
{
    public List<Person> Friends = new List<Person>();
}

But that's obviously not ideal either because it's a public field.

I could initialize in the constructor or I could probably go with lazy instantiation too:

class Person
{
    private List<Person> friends;

    public List<Person> Friends
    {
        get 
        {
            if (friends == null)
                friends = new List<Person>();
            return friends; 
        }
        set { friends = value; }
    }

}

But that's really verbose. No option seems really good in any case.

Upvotes: 1

Views: 119

Answers (5)

Mike Strobel
Mike Strobel

Reputation: 25623

If the added memory overhead isn't an issue for you (and it most likely isn't, unless you're dealing with millions of objects), the best practice is probably to skip the lazy initialization, mark the field readonly, and initialize it inline:

class Person
{
    private readonly List<Person> friends = new List<Person>();

    public List<Person> Friends
    {
        get { return friends; }
    }
}

Alternatively, the initializer can be moved into the constructor. It makes no real difference in this case; it's a stylistic decision.

There also doesn't seem to be a compelling need to allow the list itself to be replaced by external code, so just make the property read-only by omitting the set accessor.

@FelipeOriani's suggestion is very close to mine, and is a perfectly acceptable alternative. I personally prefer to enforce readonly semantics for fields whenever appropriate (particularly for collections), ensuring they can only be initialized during object creation, but this mostly a matter of personal preference.

Upvotes: 1

Tigran
Tigran

Reputation: 62246

You may also try to use Lazy construct available from 4.0 version

So your code may look like:

class Person
{
    private Lazy<List<Person>> friends = new Lazy<List<Person>>();
    public Lazy<List<Person>> Friends {
         get { return friends; }
    }
}

Upvotes: 1

Pierre Arlaud
Pierre Arlaud

Reputation: 4133

What about

class Person
{
    private List<Person> friends;

    public List<Person> Friends
    {
        get { return friends ?? (friends = new List<Person>()); }
        set { friends = value; }
    }

}

?

Upvotes: 0

dcastro
dcastro

Reputation: 68660

I (and I'm guessing most people as well) would initialize it in the constructor. Lazy initialization is only worth the trouble if the resource is expensive to create and you can't be sure whether you'll even need it.

For lists and dictionaries: just initialize them in the constructor.

Upvotes: 2

Felipe Oriani
Felipe Oriani

Reputation: 38608

As a good pratice, you can use a property with a private set and initialize on the constructor (where is the place to do this, since you create a Person object), for sample:

public class Person
{
    public List<Person> Friends { get; private set; }

    public Person() 
    {
       this.Friends = new List<Person>();
    }
}

Upvotes: 3

Related Questions