Jader Dias
Jader Dias

Reputation: 90465

Best practice when returning an array of values (.NET)

Usually my methods are as the following:

public List<int> Method1(int input)
{
    var output = new List<int>();
    //add some items to output
    return output;
}

But FxCop advises another IList implementation instead of List, but I can't remember which. The alternatives include returning it as an IList, ICollection or IEnumerable for more flexibility or a whole different way as the code below.:

public int[] Method2(int input)
{
    var output = new List<int>();
    //add some items to output
    return output.ToArray();
}

Of all alternatives, all provided and all possibilities, which is considered the best practice?

Upvotes: 3

Views: 3266

Answers (9)

Damian Mark
Damian Mark

Reputation: 1

Some exponential or polynomial algorithms have many values inside of them you want to extract without adding multiple duplicate methods that just return those separate values. Instead just return new int[] { log n, x^n, residue }.

You can create multiple methods to facade; but you only need the one algorithm to do the work. It is better if it's not recursive too.

Upvotes: 0

AwesomeTown
AwesomeTown

Reputation: 2880

Eric Lippert has a good post on why returning an array is usually a bad idea.

Typically, you should be as general as you can be without causing undue grief to whoever is going to be calling your method. Prefer interfaces over concrete classes, and pick the most generic interface you can get away with.

Returning interfaces better encapsulates your implementation, and will make things easier to change in the future. If you start with a concrete type, you're committed to always returning that type.

IEnumerable<T> is the best place to start. With the advent of LINQ, there are very few things a caller can't do easily just given an enumeration. If a caller occasionally needs a list, it's easy enough to call .ToList().

If callers are likely to have a specific need to index into the returned collection, or if they are likely going to want to modify the collection themselves (inserting/removing/reordering items), consider using an IList<T>.

Upvotes: 5

Richard
Richard

Reputation: 108975

"Framework Design Guidelines" (2nd ed) in §8.3.1 has quite a lot to say about collections as return values, summary:

  • DO NOT provide settable collection properties.
  • DO use Collection<T> or a subclass of Collection<T> for properties or return values representing read/write collections.
  • DO use ReadOnlyCollection<T>, a subclass of ReadOnlyCollection<T>, or in rare cases IEnumerable<T> for properties or return values representing read-only collections.

(and more, but these three capture the core).

The first of those above: don't return reference to internal collection unless you want the user to be able to change it (and then likely you should have a custom type so you have a degree of control).

I would return IList<T> and ensure I do not define the actual type I am returning, unless I was returning an iterator (when I would use IEnumerable<T>).

Upvotes: 5

Richard Ev
Richard Ev

Reputation: 54087

I almost always go with List because it gives me methods that I frequently find the most useful.

One potential negative consequence of returning an IEnumerable is that any exceptions that get thrown upon enumeration would come from an area of code that could be quite far away from the code that actually built the object, making bug tracking harder.

Upvotes: 1

Cerebrus
Cerebrus

Reputation: 25775

It depends on your requirement. All things said and done, iteration through strongly typed arrays is the fastest amongst all collection types. If you do not need to resize/add/search through them, arrays are quite fine.

Upvotes: 1

Kent Boogaart
Kent Boogaart

Reputation: 178630

It depends.

Do you want the caller to be able to modify the items and for you to see those changes? Arrays can be modified. The IList interface defines methods for modification (but the implementation may not allow it).

Can you elaborate on the FxCop warning?

Kent

Upvotes: 1

Joel Coehoorn
Joel Coehoorn

Reputation: 415600

ReadOnlyCollection<T> is another option.

Upvotes: 3

Inferis
Inferis

Reputation: 4652

Return the interface that you need in the code calling this method.

If you need to do list actions on the result, return IList<T>. If you just need to enumerate the results, return IEnumerable<T>.

Those are the ones I use most, actually. I never return arrays from public interfaces unless there's a very good reason for it.

Upvotes: 2

Surgical Coder
Surgical Coder

Reputation: 1084

IEnumerable / IEnumerable <T> unless you specifically need a list, then you should return IList <T>

Upvotes: 6

Related Questions