TICS Coding Standard Viewer 
TIOBE Software Quality Framework
Print-friendly version
©TIOBE Software www.tiobe.com
 
C# Coding Standard
Search

Rule:  5@119Checked automatically with code checker

Synopsis:Return an IEnumerable<T> or ICollection<T> instead of a concrete collection class
Language:C#
Severity Level:5
Category:Object lifecycle


Description:

In general, you don't want callers to be able to change an internal collection, so don't return arrays, lists or other collection classes directly. Instead, return an IEnumerable<T>, or, if the caller must be able to determine the count, an ICollection<T>.

So

    public IEnumerable<FooBar> GetRecentItems()

is preferred to

    public List<FooBar> GetRecentItems()

Note If you're using .NET 4.5, you can also use IReadOnlyCollection<T>, IReadOnlyList<T> or IReadOnlyDictionary<TKey, TValue>.



Literature References:
Aviva AV1130