<p>Prior to C# generics, everyone would code collections for their business objects by creating a collection base that implemented IEnumerable</p>

<p>IE:</p>

<pre><code>public class CollectionBase : IEnumerable
</code></pre>

<p>and then would derive their Business Object collections from that.</p>

<pre><code>public class BusinessObjectCollection : CollectionBase
</code></pre>

<p>Now with the generic list class, does anyone just use that instead? I've found that I use a compromise of the two techniques:</p>

<pre><code>public class BusinessObjectCollection : List&lt;BusinessObject&gt;
</code></pre>

<p>I do this because I like to have strongly typed names instead of just passing Lists around.</p>

<p>What is <strong>your</strong> approach?</p>

[{'answer_id': 21720, 'author': 'Matt Hamilton', 'author_id': 615, 'author_profile': 'https://Stackoverflow.com/users/615', 'pm_score': 2, 'selected': False, 'text': '<p>I generally only derive my own collection classes if I need to "add value". Like, if the collection itself needed to have some "metadata" properties tagging along with it.</p>\n'}, {'answer_id': 21722, 'author': 'Scott Muc', 'author_id': 1894, 'author_profile': 'https://Stackoverflow.com/users/1894', 'pm_score': 2, 'selected': False, 'text': "<p>I've been going back and forth on 2 options:</p>\n\n<pre><code>public class BusinessObjectCollection : List&lt;BusinessObject&gt; {}\n</code></pre>\n\n<p>or methods that just do the following:</p>\n\n<pre><code>public IEnumerable&lt;BusinessObject&gt; GetBusinessObjects();\n</code></pre>\n\n<p>The benefits of the first approach is that you can change the underlying data store without having to mess with method signatures. Unfortunately if you inherit from a collection type that removes a method from the previous implementation, then you'll have to deal with those situations throughout your code.</p>\n"}, {'answer_id': 21724, 'author': 'jeremcc', 'author_id': 1436, 'author_profile': 'https://Stackoverflow.com/users/1436', 'pm_score': 2, 'selected': False, 'text': '<p>I do the exact same thing as you Jonathan... just inherit from <code>List&lt;T&gt;</code>. You get the best of both worlds.  But I generally only do it when there is some value to add, like adding a <code>LoadAll()</code> method or whatever.</p>\n'}, {'answer_id': 21727, 'author': 'AdamSane', 'author_id': 805, 'author_profile': 'https://Stackoverflow.com/users/805', 'pm_score': 0, 'selected': False, 'text': '<p><strong>6 of 1, half dozen of another</strong> </p>\n\n<p>Either way its the same thing. I only do it when I have reason to add custom code into the BusinessObjectCollection. </p>\n\n<p>With out it having load methods return a list allows me to write more code in a common generic class and have it just work. Such as a Load method.</p>\n'}, {'answer_id': 21730, 'author': 'tghw', 'author_id': 2363, 'author_profile': 'https://Stackoverflow.com/users/2363', 'pm_score': 3, 'selected': False, 'text': "<p>I prefer just to use <code>List&lt;BusinessObject&gt;</code>.  Typedefing it just adds unnecessary boilerplate to the code.  <code>List&lt;BusinessObject&gt;</code> is a specific type, it's not just any <code>List</code> object, so it's still strongly typed.</p>\n\n<p>More importantly, declaring something <code>List&lt;BusinessObject&gt;</code> makes it easier for everyone reading the code to tell what types they are dealing with, they don't have to search through to figure out what a <code>BusinessObjectCollection</code> is and then remember that it's just a list.  By typedefing, you'll have to require a consistent (re)naming convention that everyone has to follow in order for it to make sense.</p>\n"}, {'answer_id': 21731, 'author': 'Tim Frey', 'author_id': 1471, 'author_profile': 'https://Stackoverflow.com/users/1471', 'pm_score': 2, 'selected': False, 'text': "<p>You should probably avoid creating your own collection for that purpose.  It's pretty common to want to change the type of data structure a few times during refactorings or when adding new features.  With your approach, you would wind up with a separate class for BusinessObjectList, BusinessObjectDictionary, BusinessObjectTree, etc.</p>\n\n<p>I don't really see any value in creating this class just because the classname is more readable.  Yeah, the angle bracket syntax is kind of ugly, but it's standard in C++, C# and Java, so even if you don't write code that uses it you're going to run into it all the time.</p>\n"}, {'answer_id': 21733, 'author': 'Darren Kopp', 'author_id': 77, 'author_profile': 'https://Stackoverflow.com/users/77', 'pm_score': 4, 'selected': False, 'text': '<p>It\'s <a href="http://blogs.msdn.com/fxcop/archive/2006/04/27/faq-why-does-donotexposegenericlists-recommend-that-i-expose-collection-lt-t-gt-instead-of-list-lt-t-gt-david-kean.aspx" rel="noreferrer">recommended</a> that in public API\'s not to use List&lt;T&gt;, but to use Collection&lt;T&gt;</p>\n\n<p>If you are inheriting from it though, you should be fine, afaik.</p>\n'}, {'answer_id': 21786, 'author': 'Scott Wisniewski', 'author_id': 1737192, 'author_profile': 'https://Stackoverflow.com/users/1737192', 'pm_score': 7, 'selected': True, 'text': '<p>I am generally in the camp of just using a  List directly, unless for some reason I need to encapsulate the data structure and provide a limited subset of its functionality. This is mainly because if I don\'t have a specific need for encapsulation then doing it is just a waste of time.</p>\n\n<p>However, with the aggregate initializes feature in C# 3.0, there are some new situations where I would advocate using customized collection classes.</p>\n\n<p>Basically, C# 3.0 allows any class that implements <code>IEnumerable</code> and has an Add method to use the new aggregate initializer syntax. For example, because Dictionary defines a method Add(K key, V value) it is possible to initialize a dictionary using this syntax:</p>\n\n<pre><code>var d = new Dictionary&lt;string, int&gt;\n{\n    {"hello", 0},\n    {"the answer to life the universe and everything is:", 42}\n};\n</code></pre>\n\n<p>The great thing about the feature is that it works for add methods with any number of arguments. For example, given this collection:</p>\n\n<pre><code>class c1 : IEnumerable\n{\n    void Add(int x1, int x2, int x3)\n    {\n        //...\n    }\n\n    //...\n}\n</code></pre>\n\n<p>it would be possible to initialize it like so:</p>\n\n<pre><code>var x = new c1\n{\n    {1,2,3},\n    {4,5,6}\n}\n</code></pre>\n\n<p>This can be really useful if you need to create static tables of complex objects. For example, if you were just using <code>List&lt;Customer&gt;</code> and you wanted to create a static list of customer objects you would have to create it like so:</p>\n\n<pre><code>var x = new List&lt;Customer&gt;\n{\n    new Customer("Scott Wisniewski", "555-555-5555", "Seattle", "WA"),\n    new Customer("John Doe", "555-555-1234", "Los Angeles", "CA"),\n    new Customer("Michael Scott", "555-555-8769", "Scranton PA"),\n    new Customer("Ali G", "", "Staines", "UK")\n}\n</code></pre>\n\n<p>However, if you use a customized collection, like this one:</p>\n\n<pre><code>class CustomerList  : List&lt;Customer&gt;\n{\n    public void Add(string name, string phoneNumber, string city, string stateOrCountry)\n    {\n        Add(new Customer(name, phoneNumber, city, stateOrCounter));\n    }\n}\n</code></pre>\n\n<p>You could then initialize the collection using this syntax:</p>\n\n<pre><code>var customers = new CustomerList\n{\n    {"Scott Wisniewski", "555-555-5555", "Seattle", "WA"},\n    {"John Doe", "555-555-1234", "Los Angeles", "CA"},\n    {"Michael Scott", "555-555-8769", "Scranton PA"},\n    {"Ali G", "", "Staines", "UK"}\n}\n</code></pre>\n\n<p>This has the advantage of being both easier to type and easier to read because their is no need to retype the element type name for each element. The advantage can be particularly strong if the element type is long or complex. </p>\n\n<p>That being said, this is only useful if you need static collections of data defined in your app. Some types of apps, like compilers, use them all the time. Others, like typical database apps don\'t because they load all their data from a database.</p>\n\n<p>My advice would be that if you either need to define a static collection of objects, or need to encapsulate away the collection interface,  then create a custom collection class. Otherwise I would just use <code>List&lt;T&gt;</code> directly.</p>\n'}, {'answer_id': 21806, 'author': 'Ryan Eastabrook', 'author_id': 105, 'author_profile': 'https://Stackoverflow.com/users/105', 'pm_score': 1, 'selected': False, 'text': '<p>I use generic lists for almost all scenarios.  The only time that I would consider using a derived collection anymore is if I add collection specific members.  However, the advent of LINQ has lessened the need for even that.</p>\n'}, {'answer_id': 43133, 'author': 'Matt Hinze', 'author_id': 2676, 'author_profile': 'https://Stackoverflow.com/users/2676', 'pm_score': -1, 'selected': False, 'text': '<p>this is the way:</p>\n\n<p>return arrays, accept <code>IEnumerable&lt;T&gt;</code></p>\n\n<p>=)</p>\n'}, {'answer_id': 75319, 'author': 'Joe', 'author_id': 13087, 'author_profile': 'https://Stackoverflow.com/users/13087', 'pm_score': 0, 'selected': False, 'text': '<p>As someone else pointed out, it is recommended not to expose List publicly, and FxCop will whinge if you do so.  This includes inheriting from List as in:</p>\n\n<pre><code>public MyTypeCollection : List&lt;MyType&gt;\n</code></pre>\n\n<p>In most cases public APIs will expose IList (or ICollection or IEnumerable) as appropriate.</p>\n\n<p>In cases where you want your own custom collection, you can keep FxCop quiet by inheriting from Collection instead of List.   </p>\n'}, {'answer_id': 93950, 'author': 'Hallgrim', 'author_id': 15454, 'author_profile': 'https://Stackoverflow.com/users/15454', 'pm_score': 0, 'selected': False, 'text': '<p>If you choose to create your own collection class you should check out the types in <a href="http://msdn.microsoft.com/en-us/library/system.collections.objectmodel.aspx" rel="nofollow noreferrer">System.Collections.ObjectModel Namespace</a>. </p>\n\n<p>The namespace defines base classes thare are ment to make it easier for implementers to create a custom collections.</p>\n'}, {'answer_id': 94012, 'author': 'William Yeung', 'author_id': 16371, 'author_profile': 'https://Stackoverflow.com/users/16371', 'pm_score': 0, 'selected': False, 'text': '<p>I tend to do it with my own collection if I want to shield the access to the actual list. When you are writing business objects, chance is that you need a hook to know if your object is being added/removed, in such sense I think BOCollection is better idea. Of coz if that is not required, List is more lightweight. Also you might want to check using IList to provide additional abstraction interface if you need some kind of proxying (e.g. a fake collection triggers lazy load from database)</p>\n\n<p>But... why not consider Castle ActiveRecord or any other mature ORM framework? :)</p>\n'}, {'answer_id': 94032, 'author': 'Adam Vigh', 'author_id': 1613872, 'author_profile': 'https://Stackoverflow.com/users/1613872', 'pm_score': 0, 'selected': False, 'text': "<p>At the most of the time I simply go with the List way, as it gives me all the functionality I need at the 90% of the time, and when something 'extra' is needed, I inherit from it, and code that extra bit.</p>\n"}, {'answer_id': 192059, 'author': 'Joel Coehoorn', 'author_id': 3043, 'author_profile': 'https://Stackoverflow.com/users/3043', 'pm_score': 0, 'selected': False, 'text': '<p>I would do this:</p>\n\n<pre><code>using BusinessObjectCollection = List&lt;BusinessObject&gt;;\n</code></pre>\n\n<p>This just creates an alias rather than a completely new type.  I prefer it to using List&lt;BusinessObject> directly because it leaves me free to change the underlying structure of the collection at some point in the future without changing code that uses it (as long as I provide the same properties and methods).</p>\n'}, {'answer_id': 394843, 'author': 'abatishchev', 'author_id': 41956, 'author_profile': 'https://Stackoverflow.com/users/41956', 'pm_score': 0, 'selected': False, 'text': '<p>try out this:</p>\n\n<pre><code>System.Collections.ObjectModel.Collection&lt;BusinessObject&gt;\n</code></pre>\n\n<p>it makes unnecessary to implement basic method like CollectionBase do</p>\n'}, {'answer_id': 483504, 'author': 'Ed Blackburn', 'author_id': 27962, 'author_profile': 'https://Stackoverflow.com/users/27962', 'pm_score': 2, 'selected': False, 'text': '<p>You can use both. For laziness - I mean productivity - List is a very useful class, it\'s also "comprehensive" and frankly full of YANGNI members. Coupled with the sensible argument / recommendation put forward by the <a href="http://blogs.msdn.com/fxcop/archive/2006/04/27/faq-why-does-donotexposegenericlists-recommend-that-i-expose-collection-lt-t-gt-instead-of-list-lt-t-gt-david-kean.aspx" rel="nofollow noreferrer">MSDN article</a> already linked about exposing List as a public member, I prefer the "third" way:</p>\n\n<p>Personally I use the decorator pattern to expose only what I need from List i.e:</p>\n\n<pre><code>public OrderItemCollection : IEnumerable&lt;OrderItem&gt; \n{\n    private readonly List&lt;OrderItem&gt; _orderItems = new List&lt;OrderItem&gt;();\n\n    void Add(OrderItem item)\n    {\n         _orderItems.Add(item)\n    }\n\n    //implement only the list members, which are required from your domain. \n    //ie. sum items, calculate weight etc...\n\n    private IEnumerator&lt;string&gt; Enumerator() {\n        return _orderItems.GetEnumerator();\n    }\n\n    public IEnumerator&lt;string&gt; GetEnumerator() {\n        return Enumerator();\n    }    \n}\n</code></pre>\n\n<p>Further still I\'d probably abstract OrderItemCollection into IOrderItemCollection so I can swap my implementation of IOrderItemCollection over in the future in (I may prefer to use a different inner enumerable object such as Collection or more likley for perf use a Key Value Pair collection or Set.</p>\n'}, {'answer_id': 483590, 'author': 'Anthony', 'author_id': 5599, 'author_profile': 'https://Stackoverflow.com/users/5599', 'pm_score': 2, 'selected': False, 'text': '<p>Use the type <code>List&lt;BusinessObject&gt;</code> where you have to declare a list of them. However,\nwhere you return a list of <code>BusinessObject</code>, consider returning <code>IEnumerable&lt;T&gt;</code>, <code>IList&lt;T&gt;</code> or <code>ReadOnlyCollection&lt;T&gt;</code> - i.e. return the weakest possible contract that satisfies the client. </p>\n\n<p>Where you want to "add custom code" to a list, code extension methods on the list type. Again, attach these methods to the weakest possible contract, e.g.</p>\n\n<pre><code>public static int SomeCount(this IEnumerable&lt;BusinessObject&gt; someList)\n</code></pre>\n\n<p>Of course, you can\'t and shouldn\'t add state with extension methods, so if you need to add a new property and a field behind it, use a subclass or better, a wrapper class to store this.</p>\n'}]
['https://Stackoverflow.com/questions/21715', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1965/']