2018-08-19

Collection snippet

Often, I need to create lists of data. It is obviously possible to use generic lists (List<T>), but they are not easily expandable. For example, it is not possible to remove the Add and Remove methods, it is also difficult to add an event OnChanged that would detect when a property of object (T) change.

So, we must implement our collection that inherits from CollectionBase and IEnumerable<T> to allow LINQ queries.

To simplify the creation of these collections, I’ve prepared a Snippet to include in Visual Studio (Tools / Code Snippets Manager / Import). You can easily use it (Ctrl+K, Ctrl+X) and choose the name of the class… all methods are automatically adapted.

1. Select the ClassCollection snippet (Ctrl+K, Ctrl+X).

2. Write you class name (Employee, Department, etc).

3. And use it.

Remarks: The snippet diagram class is.

File: Download the snippet ClassCollection here.

Leave a Reply

Your email address will not be published. Required fields are marked *