473,387 Members | 1,535 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Exposing Generic Lists

We have a class that has a public property that is of type List<T>. FXCop
generates a DoNotExposeGenericLists error, indicating

"System.Collections.Generic.List<Tis a generic collection designed for
performance not inheritance and, therefore, does not contain any virtual
members. The following generic collections are designed for inheritance and
should be exposed instead of System.Collections.Generic.List<T>.

* System.Collections.ObjectModel.Collection<T>
* System.Collections.ObjectModel.ReadOnlyCollection< T>
* System.Collections.ObjectModel.KeyedCollection<TKe y, TItem"

Our property is not "virtual" and thus cannot be overridden. We used
List<Tbecasue we wanted a generic array. A collection is unordered and
thus not as good a match, even if it is better designed for inheritance.

Can anyone explain in more depth why having a non-virtual, List<tproperty
is bad? Why should I care is List<Thas any virtual members when I am
simply using an instance of and _not_ inheriting from List<T>?

Thanks,
--BJ
Feb 19 '07 #1
4 7540
We have a class that has a public property that is of type List<T>.
FXCop generates a DoNotExposeGenericLists error, indicating

"System.Collections.Generic.List<Tis a generic collection designed
for performance not inheritance and, therefore, does not contain any
virtual members. The following generic collections are designed for
inheritance and should be exposed instead of
System.Collections.Generic.List<T>.

* System.Collections.ObjectModel.Collection<T>
* System.Collections.ObjectModel.ReadOnlyCollection< T>
* System.Collections.ObjectModel.KeyedCollection<TKe y, TItem"
Our property is not "virtual" and thus cannot be overridden. We used
List<Tbecasue we wanted a generic array. A collection is unordered
and thus not as good a match, even if it is better designed for
inheritance.

Can anyone explain in more depth why having a non-virtual, List<t>
property is bad? Why should I care is List<Thas any virtual members
when I am simply using an instance of and _not_ inheriting from
List<T>?
List<Tis intended to be a workhorse class used in implementation. By exposing
a List<Tin the interface of your class, you are breaking encapsulation
by giving client code access to an implementation detail of your class--especially
if your property provides direct access to the internal List<T>. For example,
this breaks encapsulation:

public class EmployeeCensus
{
private List<Employeem_Employees;

public EmployeeCensus(IEnumerable<Employeeemployees)
{
m_Employees = new List<Employee>(employees);
}

public List<EmployeeEmployees { get { return m_Employees; } }
}

Any client code of the EmployeeCensus class could easily corrupt its state
by calling methods on the List<Employeeexposed by the Employees property.
And, if client code is responsible for maintaining the state of the EmployeeCensus
class, there's a design issue.

Proper encapsulation is to *not* expose the List<Tbut to expose a Collection<T>,
ReadOnlyCollection<Tor KeyedCollection<TKey, TItemlike this:

public class EmployeeCensus
{
private List<Employeem_Employees;

public EmployeeCensus(IEnumerable<Employeeemployees)
{
m_Employees = new List<Employee>(employees);
}

public ReadOnlyCollection<EmployeeEmployees { get { return m_Employees.AsReadOnly();
} }
}

It's a shame that these classes were included in a different namespace (System.Collections.ObjectModel)
because many developers haven't discovered them yet or realize that they
should be using them.

Best Regards,
Dustin Campbell
Developer Express Inc.
Feb 19 '07 #2


"BJ Safdie" <MS***********@nospam.nospamwrote in message
news:1D**********************************@microsof t.com...
We have a class that has a public property that is of type List<T>. FXCop
generates a DoNotExposeGenericLists error, indicating

"System.Collections.Generic.List<Tis a generic collection designed for
performance not inheritance and, therefore, does not contain any virtual
members. The following generic collections are designed for inheritance
and
should be exposed instead of System.Collections.Generic.List<T>.

* System.Collections.ObjectModel.Collection<T>
* System.Collections.ObjectModel.ReadOnlyCollection< T>
* System.Collections.ObjectModel.KeyedCollection<TKe y, TItem"

Our property is not "virtual" and thus cannot be overridden. We used
List<Tbecasue we wanted a generic array. A collection is unordered and
thus not as good a match, even if it is better designed for inheritance.

Can anyone explain in more depth why having a non-virtual, List<t>
property
is bad? Why should I care is List<Thas any virtual members when I am
simply using an instance of and _not_ inheriting from List<T>?
List<Tis a particular concrete type, and as you rev your library you will
never be able to replace the return value with another type without breaking
your clients. The basic problem is that you are exposing too much internal
detail in the public contract of your class.

I would simply change the return value from List<Tto IList<T>.

David

Feb 19 '07 #3
Dustin,

This makes little sense to me. Are you really improving encapsulation by
exposing this list as a Collection<Tinstead of List<T>? I can buy the
arguement for a read only version, but we know that many things within the
framework (like databinding) would want to get to the AddNew() methods.

If you really want to hide the implementation, I would say an argument could
be made for declare your property as IList<T instead of the concrete
List<T>.

-Casey

"Dustin Campbell" wrote:
We have a class that has a public property that is of type List<T>.
FXCop generates a DoNotExposeGenericLists error, indicating

"System.Collections.Generic.List<Tis a generic collection designed
for performance not inheritance and, therefore, does not contain any
virtual members. The following generic collections are designed for
inheritance and should be exposed instead of
System.Collections.Generic.List<T>.

* System.Collections.ObjectModel.Collection<T>
* System.Collections.ObjectModel.ReadOnlyCollection< T>
* System.Collections.ObjectModel.KeyedCollection<TKe y, TItem"
Our property is not "virtual" and thus cannot be overridden. We used
List<Tbecasue we wanted a generic array. A collection is unordered
and thus not as good a match, even if it is better designed for
inheritance.

Can anyone explain in more depth why having a non-virtual, List<t>
property is bad? Why should I care is List<Thas any virtual members
when I am simply using an instance of and _not_ inheriting from
List<T>?

List<Tis intended to be a workhorse class used in implementation. By exposing
a List<Tin the interface of your class, you are breaking encapsulation
by giving client code access to an implementation detail of your class--especially
if your property provides direct access to the internal List<T>. For example,
this breaks encapsulation:

public class EmployeeCensus
{
private List<Employeem_Employees;

public EmployeeCensus(IEnumerable<Employeeemployees)
{
m_Employees = new List<Employee>(employees);
}

public List<EmployeeEmployees { get { return m_Employees; } }
}

Any client code of the EmployeeCensus class could easily corrupt its state
by calling methods on the List<Employeeexposed by the Employees property.
And, if client code is responsible for maintaining the state of the EmployeeCensus
class, there's a design issue.

Proper encapsulation is to *not* expose the List<Tbut to expose a Collection<T>,
ReadOnlyCollection<Tor KeyedCollection<TKey, TItemlike this:

public class EmployeeCensus
{
private List<Employeem_Employees;

public EmployeeCensus(IEnumerable<Employeeemployees)
{
m_Employees = new List<Employee>(employees);
}

public ReadOnlyCollection<EmployeeEmployees { get { return m_Employees.AsReadOnly();
} }
}

It's a shame that these classes were included in a different namespace (System.Collections.ObjectModel)
because many developers haven't discovered them yet or realize that they
should be using them.

Best Regards,
Dustin Campbell
Developer Express Inc.
Feb 19 '07 #4
This makes little sense to me. Are you really improving encapsulation
by exposing this list as a Collection<Tinstead of List<T>? I can
buy the arguement for a read only version, but we know that many
things within the framework (like databinding) would want to get to
the AddNew() methods.

If you really want to hide the implementation, I would say an argument
could be made for declare your property as IList<T instead of the
concrete List<T>.
Yes, you can improve encapsulation by exposing a Collection<T>. Collection<T>
is designed for inheritance. It provides virtual methods that can be overridden
to have complete control over what will happen when items are added and removed.
Because of that, it *can* be safe to expose. If there is a need to expose
a list-type data structure that a client can manipulate (e.g. add and remove
items from), a Collection<Tdescendent is probably the way to go. Granted,
I think the need to do that is extremely rare so I would normally opt for
a ReadOnlyCollection<T>. Besides, there's methods on List<Tand Array to
produce a ReadOnlyCollection<Tso it's easy.

As for IList<T>, if you expose that, you are really only protecting one thing:
the type of data structure that you used internally. While it's true that
you could change the type of data structure that you are using later, you
have exposed an interface that allows clients to manipulate the state of
your class in a way that the class doesn't have any control over it. IList<T>
has Add and Remove methods that won't notify your class when called. So,
you're still breaking encapsulation by giving clients direct access to your
class's private parts. Just because you use an interface doesn't mean it's
OK.

Best Regards,
Dustin Campbell
Developer Express Inc.
Feb 20 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: tascien | last post by:
Hi guys, I have a class object I want to expose in a webservice class... How can I make sure that all objects including subclasses are exposed in WSDL. here is an example: Public Class Lists '<...
11
by: ZenRhapsody | last post by:
Has anyone done any performance testing between new generic Lists and single dimensional arrays? I really like the code flexibility the List provides since I don't know how many items I will...
2
by: Greg Buchholz | last post by:
/* I've been experimenting with some generic/polytypic programs, and I've stumbled on to a problem that I can't quite figure out. In the program below, I'm trying to define a generic version of...
0
by: Wiktor Zychla [C# MVP] | last post by:
We do have generic classes, methods and delegates. My question is: what reason prevents us from having generic properties and indexers? // impossible public List<T> GetList<T> { get { ... }
5
by: Andrew Ducker | last post by:
I have something I'm trying to make work with generics, and it seems like it should, but I can't quite get there. I have a subclass of List<Tthat I want to populate automatically. Each different...
3
by: Seth Gecko | last post by:
Hi I am working with generic lists of various objects and a control dealing with these lists. For instance: A parent form holds: dim Walls as List(Of wall) dim Segments as List(Of segment) ...
1
by: Suds | last post by:
Hi, I'm having an issue with invoking a Generic method that takes Generic Arguments. My method signature is public void GenericMethodWithGenericArguments<E, V>(List<EtheFirstList,...
1
by: Jeff | last post by:
..NET 2.0 Is generic lists faster then tradional lists when sending over a collection of objects (value by reference) in .NET remoting. Lets say if a list of object should be sent from a...
0
by: jappenzeller | last post by:
I've got a coding standard question for generic lists and .NET 2.0. I have created some custom strongly typed lists like, MyClassList:List<MyClass>. We did this because we wanted to reuse some...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.