I think that the question when to use IEnumerable, ICollection, IList or List is a common one that hasn’t often being answered in an easy way. I not only want to do this within this article, but I also want to give you some further information to understand the things more deeply.
If you get the understanding for the principle, you’ll be automatically really confident when you have to do this decision for the next time.
If you only want to know when to use which type, scroll down and have a look at the table providing the scenarios and the relevant types. I strongly recommend reading of the entire article to get a deeper understanding.
Let’s first take a look at the individual types and more importantly its members. It’s generally a good idea to have a look at the types in question if you want to decide which type you should use in a specific situation in your code.
IEnumerable
The following example implements the ICollection interface to create a collection of custom Box objects named BoxCollection.Each Box has height, length, and width properties, which are used to define equality. Apr 07, 2021 DMG Master is a simple and optimizedmacOS app to create standard DMG Disk Image Archive recognized by any Mac, with just one click. Disk Images Archive (DMG) is the preferred and standard way on the Mac to create archives to exchange group of files compacted in a single file from one user to another, is also the preferred way to distribute application. ICollections is an expertly designed application which helps you keep your desktop organized. The app's unique features are what make it the most popular method of desktop organization for Mac users. ICollections lets you create areas on your desktop so you can sort and arrange your icons.
First of all, it is important to understand, that there are two different interfaces defined in the .NET base class library. There is a non-generic IEnumerable interface and there is a generic type-safe IEnumerable<T> interface.
The IEnumerable interface is located in the System.Collections namespace and contains only a single method definition. The interface definition looks like this:
The GetEnumerator method must return an instance of an object of a class which implements the IEnumerator interface. We won’t have a look at the definition of the IEnumerator interface this time, but if you are interested, please visit the official msdn documentation.
It is important to know that the C# language foreach keyword works with all types that implement the IEnumerable interface. Only in C# it also works with things that don’t explicitly implement IEnumerable or IEnumerable<T>. I believe you have been using the foreach keyword many times and without worrying about the reason why and how it worked with that type.
IEnumerable<T>
Let’s now take a look at the definition of the generic and type-safe version called IEnumerable<T> which is located in the System.Collections.Generic namespace:
As you can see the IEnumerable<T> interface inherits from the IEnumerable interface. Therefore a type which implements IEnumerable<T> has also to implement the members of IEnumerable.
IEnumerable<T> defines a single method GetEnumerator which returns an instance of an object that implements the IEnumerator<T> interface. We won’t have a look at this interface this time. Please take a look at the official msdn documentation if you would like to get some more information.
ICollection
As you can imagine, there are also two versions of ICollection which are System.Collections.ICollection and the generic version System.Collections.Generic.ICollection<T>.
Let’s take a look at the definition of the ICollection interface type:
ICollection inherits from IEnumerable. You therefore have all members from the IEnumerable interface implemented in all classes that implement the ICollection interface.
I won’t go much into details of the defined methods and properties this time. I just want to let you know about the official description from the msdn documentation:
Defines size, enumerators, and synchronization methods for all nongeneric collections.
ICollection<T>
Starbucks barista coffee grinder manual. When we look at the generic version of ICollection, you’ll recognize that it does not look exactly the same as the non-generic equivalent:
The official msdn documentation looks like this:
Defines methods to manipulate generic collections.
In fact, we have some more methods to add, remove and clear a collection. The way synchronization was implemented differs also. I believe that this happened because the generic version of this interface was introduced with .NET 2.0 whereas the non-generic version was already introduced in .NET 1.1.
IList
The IList interface has of course a non-generic and a generic version. We start with looking at the non-generic IList interface:
IList implements ICollection and IEnumerable. In addition it provides method definitions for adding and removing elements and to clear the collection. It also provides methods for handling the positioning of the elements within the collection. It also provides an object indexer to allow the user to access the collection with square brackets like:
IList<T>
Now let’s take a look at the generic version of IList:
As we mentioned before when discussing the ICollection<T> interface, there are some more methods defined in the ICollection<T> interface than in the ICollection interface. Therefore the member list of the IList<T> interface is a bit shorter than the non-generic equivalent. Autel maxidiag eu702 software update. We only have some new methods for accessing a collection with specific positioning.
Conclusion
Take a look at the following graphic. Not every interface member is displayed, but it should be enough to give you an overview about all types we discussed.
Which type should you depend on?
Now that we have looked at all of the interfaces in question, we are ready to decide which interface we should depend on in a certain situation. Generally, it’s a great idea to depend only on things we really need. I am going to show you how this decision can be made very easily and what you can expect to gain if you do so.
If you use a narrower interface type such as IEnumerable instead of IList, you protect your code against breaking changes. If you use IEnumerable, the caller of your method can provide any object which implements the IEnumerable interface. These are nearly all collection types of the base class library and in addition many custom defined types. The caller code can be changed in the future and your code won’t break that easily as it would if you had used ICollection or even worse IList.
If you use a wider interface type such as IList, you are more in danger of breaking code changes. If someone wants to call your method with a custom defined object which only implements IEnumerable, it simply won’t work and will result in a compilation error.
Generally you should always use that type that provides a contract for only the methods you really use.
The following table gives you an overview of how you can decide which type you should depend on.
Interface | Scenario |
IEnumerable, IEnumerable<T> | The only thing you want is to iterate over the elements in a collection. You only need read-only access to that collection. |
ICollection, ICollection<T> | You want to modify the collection or you care about its size. |
IList, IList<T> | You want to modify the collection and you care about the ordering and / or positioning of the elements in the collection. |
List, List<T> | Since in object oriented design you want to depend on abstractions instead of implementations, you should never have a member of your own implementations with the concrete type List/List. |
Recommendation
If you are now curious about interfaces, how they work, how to implement them on your own and how their use can improve your design and therefore the quality of your entire code base, I highly recommend watching C# Interfaces by Jeremy Clark on Pluralsight. He manages to explain several really important concepts in a very easily understandable way.
By clicking 'Continue to App' button you accept the Privacy Policy
|
iCollections 6.6 Shareware
Recommended Applications for Windows
About iCollections
iCollections is a skillfully designed application that helps keep your Mac organized as you would a physical desk at your workstation. It is intended to take your organization process to a level that makes your desktop neat and very attractive.
iCollections requires macOS X10.12 and fully compatible with macOS Catalina. It allows for the creation of collections for organizing desktop items, disk panels for disk drives, folder views for folders and even photo frames for favorite images all accessible from the desktop.
iCollections Organizing icons on your desktop:
With few clicks, the icons, files, and folders on your desktop will get grouped according to your preferences. Thus, the days of scouring your desktop for a particular application are over. Just get started by creating a new Collection by accessing the iCollections main menu from the system toolbar.
iCollections is fully integrated with your macOS such that it launches immediately on start up as this is the default option which you should choose, thus allowing you to concentrate your mental energy into your work straight away.
iCollections Revealing your drives:
iCollections allows you to view all drives connected to your Mac without having to navigate to be sure whats been connected and whats not with its Disk Panel and therefore gives you the right to filter what disks that you see.
iCollection Favorite Websites at your Fingertips:
The need to open your browser to check out that instant chat that just came in that cant wait, or the new video that just dropped from your favorite artist is now easier with iCollections. As it helps you view and keep in constant touch with the website you are most active on, without opening your web browser from scratch.
iCollection Power to Customize
iCollections boasts of a very detailed customization setting for its appearance.
Technical Details about iCollections
Desktop
2020-10-01
Naarak Studio