Use of 'List<T>' in the framework...

Use of 'List<T>' in the framework...

Old forum URL: forums.lhotka.net/forums/t/1452.aspx


hammdo posted on Wednesday, October 11, 2006

I'm a huge fan of the framework.  Part of my normal practice is running fxCop on code.  While most of the time, I don't have any real 'issue', today I ran into a Microsoft reference to the use of List:

Error, Certainty 95, for DoNotExposeGenericLists

{

Target : GetFields(System.String,System.String,System.String):System.Collections.Generic.List`1<Csla.Web.Design.ObjectFieldInfo> (IntrospectionTargetMethodBase)

Location : file:///C:/Code%2520Tools/CSLA/CSLA.NET%25202.1/csla20cs/csla20cs/Csla/Web/Design/TypeLoader.cs<183> (String)

Resolution : "Change 'System.Collections.Generic.List`1<Csla.Web.Design.ObjectFieldInfo>'

in TypeLoader.GetFields(String, String, String):List`1<Csla.Web.Design.ObjectFieldInfo>

to use Collection<T>, ReadOnlyCollection<T> or KeyedCollection<K,

V>"

Help : http://www.gotdotnet.com/team/fxcop/docs/rules.aspx?version=1.35&url=/Design/DoNotExposeGenericLists.html (String)

Category : Microsoft.Design (String)

CheckId : CA1002 (String)

RuleFile : Design Rules (String)

Info : "Do not expose List<T> in object models. Use Collection<T>,

ReadOnlyCollection<T> or KeyedCollection<K,V> instead.

List<T> is meant to be used from implementation, not

in object model API. List<T> is optimized for performance

at the cost of long term versioning. For example, if

you return List<T> to the client code, you will not

ever be able to receive notifications when client code

modifies the collection."

Created : 10/11/2006 5:22:22 PM (DateTime)

LastSeen : 10/11/2006 5:46:21 PM (DateTime)

Status : Active (MessageStatus)

Fix Category : Breaking (FixCategories)

}

 

I'm sure there was a very valid reason for List<T>.  I've read the book, but I did not see it.  Any info would be great.

 

Thanks for a great framework!

-Don

RockfordLhotka replied on Wednesday, October 11, 2006

I just picked the lightest weight collection type that did what I needed. Technically ObjectFieldInfo could be non-public - it isn't needed by the outside world. But of course, Microsoft actually implemented this functionality in .NET - and didn't make it public - which is why I had to write it in the first place. Frustrating... But perhaps they didn't make it public so they could use List<T> and not worry about FxCop? Smile [:)]

hammdo replied on Thursday, October 12, 2006

Thanks Rocky!  I know how it is sometimes with MS ' do as I say, not as I do' (I think my parents used that one on me too...).  I don't disagree with your approach -- I figured it was because it was fast and light -- it just struck me that fxCop would complain about it when in their own explanation, they state is was fast and light weight.

Copyright (c) Marimer LLC