The Best C# Collections Explained
C# 12 expands aliases to tuples, pointers, arrays, and most other types, helping shorten repeated or complex declarations.
Transcript 4 topics
Choose the right collection
0:00When you're working with collections in c sharp, you have several options. I collection, I list, I enumerable, I queryable. Which one's the right choice? It depends on your application. Let's go through each and decide which is right for you.
Now innumerable is the most basic of all of them. In fact, the rest of them inherit from innumerable. But because of that, you don't get some of the features that the others have. For instance, you don't get things like positional operations. You can't access items by their index.
And like I said, they're only forward based so you can't go backwards. But they're great for iterating through things that are in memory already. So the for each loop that uses the built in get a numerator move next all those functions of an IEnumerable to navigate through them.
Use ICollection for mutations
0:43Now on top of an IEnumerable is the I Collection. One of the benefits that I Collection adds on top of IEnumerable is the ability to add and remove items. Icollection is great when you don't need positional things, no index related objects. So you can use dot add, dot remove, dot contains, or dot copy to. And it even adds the dot count method.
So you get that additional functionality of being able to add and remove items and move around a bit but not positionally. I list builds on iCollection and I guess you can kind of assume where we're going with this.
Use IList for indexing
1:16Yeah, it adds the ability to access items based on their index. So because I list extends iCollection you still have the ability to add and remove but you gain the ability to insert which allows you to add at a certain position or remove add which allows you to remove at a specific position. It also adds the ability to just access items based on position. So if you had list bracket number bracket, that's gonna get you the item at that position. Lastly, let's talk about IQueryable.
Now this one's a little bit different. It doesn't live in the same namespace. This one lives in system. Link, but it does extend the IEnumerable interface.
Reserve IQueryable for databases
1:54Now you've likely seen some code where somebody is using an IEnumerable or an IQueryable when accessing say a database. And while you can do that, you shouldn't. IEnumerable should be kept for things that are in memory. Why? Well, because it does all of its sorting and filtering in memory.
So if you send off a database request with an IEnumerable, what it's going to do is give you all the records back and then it's going to filter and sort inside of memory. It's not going to send that WHERE clause with your request to the database. Okay. So editor Michael stepping in here for a minute. I misspoke there.
So when you use an IEnumerable, it will transpose that filter down to a where statement and send that to the database. But what it won't transpose is any like using top two, if you're using a skip or a take, and it won't transpose any order by clauses. So those sorting options are gonna happen on the memory side where iQueryable will send them both. Now back to your regularly scheduled programming. And that's the big difference between IQueryable and IEnumerable.
An IQueryable is going to send the entire filtering, sorting request to the database. So your SQL that's going to get rendered out if you were using SQL would be select whatever, where, whatever, order by whatever. All of that happens on the database side. And of course for large data sets you can see where that would be beneficial. So what's the best time to use any of these?
Well if you have data that's in memory and you just need to iterate through it and really not even mutate it, IEnumerable is the best choice here. But if you do need to make some changes adding and inserting items then upgrade to the eye collection. And finally if you need to access things based on their position inserting or moving or just getting things that's when you want the eye list. Now all of this is kind of trumped by hey, are you pulling from a database or external service? Then you're gonna want the I Queryable so that it can do all that processing on the other side.
Now a lot of times you'll see people using an I Queryable when they're accessing external data sources like a database. But once they get that result back, you'll see them translate that into a list which is more efficient to handle because it's in memory already and just pass that around. Hopefully you've got a better understanding now of what each is for and when to use them and when not to. Until next time.
Sign in to join in. Reading needs nothing.