Custom Made Collection
I guess this is a real stupid question but how do I create my own custom collection?
I have a
Player
class and I need to create a custom class that is a Collection of Player with other fields and methods but how do I implement my own collection in .NET Core?
If I had to guess, I would say implement IEnumerable<T>
and IEnumerator<T>
but I don't know how to implement them either. Can I have some help please?
I can provide with a more in-depth of the idea and my classes if needed.58 Replies
Just implement the interface you need or inherit from the type you want, yes
Inheritance might be easier in this case, if you don't need to add much
Can I have an example?
The problem is that I need a Collection that acts like a Queue in the sense of FIFO but I need to be able to remove Player at any position and have all LINQ methods like First<>(), Any<>() etc
(It won't be a generic collection tho)
Angius
REPL Result: Success
Result: int
Compile: 508.740ms | Execution: 75.859ms | React with ❌ to remove this embed.
But that doesn't guarantee order of insertion right?
No, you would need a sorted collection to guarantee order
So what would you recommend?
SortedList
And I can do the same? Just add more fields if needed and methods?
You could also inherit
Queue
if you need a queue as you say
And have it implement IList
to get .Add()
etcThe queue was just so I have a FIFO collection
I need a Collection that has the FIFO property, can remove people at any index in the collection, maintains insertion order and can be modified if needed
In that case I inherit Queue and IList or SortedList only should work?
Queue should ensure order, and gives you FIFO, so that's an obvious choice, yeah
IList
for adding and removing at arbitrary pointsSo instead of doing my Custom Collection I just implement and adapt?
Well, this will be your custom collection
I mean, I don't need to do from scratch implementing only IEnumerable and IEnumerator
Yeah
Ok
Tysm
Is there any tutorial or document that help me understand what each method implementation should look like?
I'll do you one better than tutorials
https://learn.microsoft.com/en-us/dotnet/api/system.collections.generic.list-1?view=net-8.0
This shows the source code of
List
Here's how .RemoveAt()
is implemented, for example: https://github.com/dotnet/runtime/blob/5535e31a712343a63f5d7d796cd874e563e5ac14/src/libraries/System.Private.CoreLib/src/System/Collections/Generic/List.cs#L961-L977But how do I implement them on top of the Queue I inherited?
Queue implements IEnumerable
You can use that
Because the Class is like this:
Or... huh
I wonder if it would be easier to just skip the queue, back your collection with an array or some such, and just add whatever methods you need
Like, I assume,
Dequeue()
Arrays, IIRC, do have a guaranteed order of elementsWait, I don't think I follow the new idea
Something like that, I'd imagine
But then I wouldn't have assured order of insertion, right?
I would have FIFO tho
Why would the order not be assured?
You'd be able to implement
.Add()
however you wantBecause you are using an IList
But what actually stores the data is an array
Actually, pretty sure even a
List
is orderedI could just use the SortedList right?
You could, yeah
Sorry but how do I inherit SortedList but of type Player?
Because it says it has a Key and a Value?
The key could be an integer, for example
The key is what it's sorted by
So like an index?
Yeah
So like this for example?
That could work, yeah
But thats what you had in mind about altering its functions?
And thats how you would implement?
What I had in mind talking about altering functions, is that you have full control over what does what. You could have
.Add()
insert the item at a random index, or you could have .Remove()
duplicate every item
Wouldn't make much sense, but you could do it
As for the above code, yeah, that's probably how I'd do it if I needed a sorted collection with a .Pop()
Ok, Ill try to implement it more tysm
wait, I'm confused here. you're talking about ensuring the order of insertion via FIFO, and using a sortedlist
No, I needed a collection that had the properties I mentioned
And if I had to create one from scratch, how should I
But I think just creating a collection that inherits from SortedList with extra methods is going to work fine
a sorted list ensures, regardless of the order you add items, the values are sorted by key
I actually ended up with this by now:
but then you can't insert at a given index...
have you tested what you're trying to do? using a sortedset just means you have some other functionality to maintain the order of your Player list.
I need to maintain the order and I don't need to insert in the middle or first position
Always in the last
you want to use a queue with remove at functionality, I'd implement a class that internally has a queue and, when remove at is called in my class, turn the queue into an array to get how many items exist after the index to be removed, dequeue them to a local var, dequeue the one i don't want and requeue them.
Isn't that less efficient?
May I ask how to implement an Intersect method for my class that has a
target
parameter that is another IEnumerable
of the same type and a delegate
that is what field I am comparing?
This is the method:
What is the second param?
I am confusedless efficient than what? I'm driving now but I'm pretty sure, if you implement your code as is, insert 5 players in a given order then remove the middle one and add it back in and look at the order of your players. then add the exact same 5 players in a different order, remove the middle and add it back at the end. you'll see the order of the list of your players is the same
Than using a SortedList as the only method I need that relates it to a Queue is the Dequeue as SortedList Add() already inserts in the last index right?
no
Add will add your item, then internally sort it's indexes to match the order of your keys
So it doesn't maintain order of insertion?
so if it's an <int, Player> and you pass (2, Apple), (4, Jacks), (3, Tomato soup), the order will be (2, Apple), (3, Tomato soup), (4, Jacks)
ok. I'm moving now. but just test this, I'm like 90% sure I'm right.
I am using SortedSet<Player> I don't have an index so wouldn't it ensure that?
Ok
85%. I've never used a sortedlist and it doesn't sound like what you need either
Ok I see it now
Does a normal HashSet<T> guarantees order of insertion?
Nevermind
A normal List<T> should work then? 🤔
Ok, I ended up with this so far and it seems to work:
But I would need help with this
.Intersect()
is a LINQ method, you don't implement it
And the second param is the equality comparer
It's optional, too
https://learn.microsoft.com/en-us/dotnet/api/system.linq.enumerable.intersect?view=net-8.0#system-linq-enumerable-intersect-1(system-collections-generic-ienumerable((-0))-system-collections-generic-ienumerable((-0))-system-collections-generic-iequalitycomparer((-0)))I ended up doing it this way but I didn't have time to test it yet:
As the way to compare two Lists and Players will always be this way