Why not set List (Of String) in the web service settings in VB.NET?

Possible duplicate:
C #: difference between List <T> and Collection <T> (CA1002, Do not expose shared lists)

FxCop says in the rule that the general list should not be exposed to the outside world.

But I don’t understand why and what is the replacement of the Generices list?

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

+4
source share
3 answers

The reason is that using a specific List<T> intended to detail the implementation, and you have to show something more abstract, like IEnumerable<T> or ICollection<T> , which represents only the functionality that you want to expose (e.g. enumerated , mutable and / or indexed). This gives you the flexibility to further change your implementation.

In practice, this warning is often resolved by returning IList<T> instead of List<T> , but the idea is to encourage you to think about "what kind of functionality do I really need to ensure that my subscribers?" For instance. maybe I should return IEnumerable<T> or ReadOnlyCollection<T> because I do not want my callers to mess around with the returned collection.

+13
source

Always show the lower base class / interface in an object hierarchy that is appropriate for your scenario. For example, if users of this property are only going to iterate over, use IEnumerable(Of T) . By exposing List<T> , you break encapsulation by giving client code access to implementation details of your class.

+2
source

A generic list is a .NET constructor, but web services are often designed to interact with other frameworks.

+1
source

All Articles