David
David

Reputation: 5537

How to initialize a field that accepts an IEnumerable

I have a property public IEnumerable<T> PieceMovement { get; set; }

what object should I use to initialize this property. I'm thinking that the simpliest .net implementation of IEnumerable will do but what would that object be and is this the right idea?

Upvotes: 1

Views: 1342

Answers (2)

Terenzio Berni
Terenzio Berni

Reputation: 495

This question is not strictly related to IEnumerable but to good OOP design practices in general.

Since you are exposing PieceMovement as an IEnumerable every object which implements IEnumerable is a good candidate.

You should never make assoumptions of the actual type so your idea is good to me. I also usually use List

Upvotes: 1

SLaks
SLaks

Reputation: 887375

You should use a List<T> or a ReadOnlyCollection<T>.

If you're initializing it based on an existing sequence, you could set it to a LINQ query.

Upvotes: 1

Related Questions