Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why not expose List(Of String) in the parameters of web service in VB.NET? [duplicate]

Possible Duplicate:
C#: Difference between List<T> and Collection<T> (CA1002, Do not expose generic lists)

The FxCop says in a rule that generic List should not be exposed to the outside world.

But I do not understand why and what is the replacement for the generice List?

Reference : http://msdn.microsoft.com/en-in/library/ms182142%28en-us%29.aspx

like image 963
Jey Geethan Avatar asked Dec 05 '22 04:12

Jey Geethan


1 Answers

The reason is that the use of a concrete List<T> is meant to be an implementation detail, and you're meant to expose something more abstract, such as IEnumerable<T> or ICollection<T>, that represents only the functionality you want to expose (such as being enumerable, mutable and/or indexable). This gives you flexibility to change the implementation later on.

In practice, this warning is often resolved by returning IList<T> instead of List<T>, but the idea is to prompt you to think about "what functionality do I actually need to guarantee my callers?" E.g. maybe I should be returning IEnumerable<T> or ReadOnlyCollection<T> because I don't want my callers messing with the returned collection.

like image 63
itowlson Avatar answered May 12 '23 22:05

itowlson