how to Implement the IReadOnlyList interface to let other classes read from your internal list.
122 Replies
Well, I would start by letting VS auto-do most of it for you. Press
ctrl+.
on IReadOnlyList
and select one of the quickfixes offered for implementing the interfaceUnknown UserOP•4y ago
Message Not Public
Sign In & Join Server To View
Well, if you're learning, see what they do 🙂
There are two for implementing the interface
Unknown UserOP•4y ago
Message Not Public
Sign In & Join Server To View
Which one did you do?
Unknown UserOP•4y ago
Message Not Public
Sign In & Join Server To View
Well, what about what it generated confuses you? I can explain stuff, but only if you tell me what you don't get
Unknown UserOP•4y ago
Message Not Public
Sign In & Join Server To View
Interfaces have nothing to do with constructors
Unknown UserOP•4y ago
Message Not Public
Sign In & Join Server To View
What is
it
here?an enumerator is something you can iterate
or more accurately enumerate
so
foreach
for example works on enumerators
so does linqUnknown UserOP•4y ago
Message Not Public
Sign In & Join Server To View
I don't know? I'm asking you?
You said I'm not totally sure what it does
I don't know what it you meant
Unknown UserOP•4y ago
Message Not Public
Sign In & Join Server To View
Again, that's a lot of code. Which things confuse you
Unknown UserOP•4y ago
Message Not Public
Sign In & Join Server To View
Well, in a real application I'd tell you just use the
List<Pokemon>
and delete the rest of the codeUnknown UserOP•4y ago
Message Not Public
Sign In & Join Server To View
But the idea is that you're trying to make
PokemonParty
represent a list (not the C# type, the English concept)Is there a particular reason you want to implement
IReadOnlyList
?By real application I meant one for production. Not one for learning
just to learn it? Because if it's an exercise, go for it. But practically speaking you wouldn't do that in this case.
Yeah, that's what I meant
Are you generally familiar with what an interface is at all?
Unknown UserOP•4y ago
Message Not Public
Sign In & Join Server To View
An interface is a contract
It says "I promise that the type that implements me has these methods and properties, and you can call them"
Unknown UserOP•4y ago
Message Not Public
Sign In & Join Server To View
It allows you to do what's called
encapsulation
, which is making sure that implementations aren't tied together
For example, let's say you have a list of pokemon
Maybe that list is implemented by List<Pokemon>
Maybe it's actually stored in a database, and calling list[1]
needs to go look in the database to find it
You don't know, and you don't care: all you know is that you have something that logically represents a list and can be indexedUnknown UserOP•4y ago
Message Not Public
Sign In & Join Server To View
And, importantly, you can easily switch it out without the thing that uses the list caring
Unknown UserOP•4y ago
Message Not Public
Sign In & Join Server To View
You wouldn't. There are built-in objects that already implement
IList<T>
. Like List<T>
. You can of course make your own class that implements IList<T>
and then pass it into anything that takes the interface, but there would be no reason to.
In your caseWell, you're trying to implement
IReadOnlyList<Pokemon>
. So you need to comply with the contract that it says you must have
And that means implementing the methods and properties defined in the interfaceUnknown UserOP•4y ago
Message Not Public
Sign In & Join Server To View
You can let the IDE scaffold it for you
Unknown UserOP•4y ago
Message Not Public
Sign In & Join Server To View
Well, all the stuff the IDE generated? It put the methods and properties in for your
Now you need to give them actual bodies
Rather than just throwing
NotImplementedException
sIf you derive from the interface, the IDE will show you some squiggles under the interface name. One of the options is "Implement Interface", which will create all the framework for the stuff the interface needs. Then, as @Orannis says, you have to actually write the code.
That was the start of this conversation 🙂
Yeah but I got the sense they haven't even gotten that far
Of course I could be wrong!
oh nm I see it up there. Must have missed it XD
Unknown UserOP•4y ago
Message Not Public
Sign In & Join Server To View
Well, you need to store them in something
I think that will do it
(I haven't tested)
doing that will implement the interface. But of course you still need to add functions to add and remove Pokemon from your
internalList
Unknown UserOP•4y ago
Message Not Public
Sign In & Join Server To View
You should move
internalList
to your class
I revised the codeUnknown UserOP•4y ago
Message Not Public
Sign In & Join Server To View
Did you see where I moved it above? It's right after the class declaration.
Unknown UserOP•4y ago
Message Not Public
Sign In & Join Server To View
No. Do you know what
static
means?Unknown UserOP•4y ago
Message Not Public
Sign In & Join Server To View
it wants a reference?
what do you mean by that?
Unknown UserOP•4y ago
Message Not Public
Sign In & Join Server To View
you shouldn't be putting the list in another class
why do you want to do that?
Unknown UserOP•4y ago
Message Not Public
Sign In & Join Server To View
okay cool
Unknown UserOP•4y ago
Message Not Public
Sign In & Join Server To View
those aren't the adding and removing functions.
GetEnumerator
returns a (you guessed it) enumerator that allows you to iterate through the items in the list. It's the thing that allows you to use foreach
on something, for example.
For adding/removing, you just need to create the appropriate functions. Something like this is a baseline:
Unknown UserOP•4y ago
Message Not Public
Sign In & Join Server To View
I think that probably covers the basics, yes.
Unknown UserOP•4y ago
Message Not Public
Sign In & Join Server To View
Well, no, you don't strictly need the interface. This is why we were saying earlier that for this use case, the interface is not helpful.
But I suppose as an exercise to learning about interfaces and how to use them, it's instructive.
Consider a function like this:
You can call this function like so:
As you can see, your function
IsPokemonInParty
is agnostic. All it cares about is the interface. We passed it a PokemonParty
and it works, because PokemonParty
implements IReadOnlyList<Pokemon>
. You can pass it literally any other object that implements IReadOnlyList<Pokemon>
.Unknown UserOP•4y ago
Message Not Public
Sign In & Join Server To View
what values?
Unknown UserOP•4y ago
Message Not Public
Sign In & Join Server To View
any
Pokemon
you put into the list will retain its values, yes.Unknown UserOP•4y ago
Message Not Public
Sign In & Join Server To View
If you want, yes
Unknown UserOP•4y ago
Message Not Public
Sign In & Join Server To View
Make sure your classes are in different files. Your
Program.cs
shouldn't contain any classes.Unknown UserOP•4y ago
Message Not Public
Sign In & Join Server To View
could you show how you print it? just to give you the correct answer
Unknown UserOP•4y ago
Message Not Public
Sign In & Join Server To View
yeah. what is done here is that, console.writeline if gets an object(and many parts in c# that output strings) uses .ToString() onto that. and as default you get the fully qualified Name
so you have 2 solutions:
- override ToString() in your pokemon class
- print and format the properties yourself where you are outputting it. Console.WriteLine($"{Pokemon.Name} {Pokemon.Power} ... ");
it depends on what you want. if its just debug info, override tostring and format it pretty
thats how a override could look like

Unknown UserOP•4y ago
Message Not Public
Sign In & Join Server To View
i dont know how your pokemon class looks like. is it posted above?
mine was just a example how to code it. property names may differ
Unknown UserOP•4y ago
Message Not Public
Sign In & Join Server To View
yeah. i dont know if your pokemon class has a propperty called "Name".
Unknown UserOP•4y ago
Message Not Public
Sign In & Join Server To View
this is just how you instanciate it. can you show the class itself? i have a suspicion what is going on
Unknown UserOP•4y ago
Message Not Public
Sign In & Join Server To View
hmm Name should be accessible. can you show me your new Console.WriteLine line?
Unknown UserOP•4y ago
Message Not Public
Sign In & Join Server To View
now the output. i want to see code 😉
Unknown UserOP•4y ago
Message Not Public
Sign In & Join Server To View
i dont seh the mentioned .Name here.
could you show how you did try to write it?
Unknown UserOP•4y ago
Message Not Public
Sign In & Join Server To View
show me how you would write it
Unknown UserOP•4y ago
Message Not Public
Sign In & Join Server To View
myobject is never in your foreace declared..
in THIS code. change line 2 into how you would write the .Name property
g2g. i look here later (promised)
Unknown UserOP•4y ago
Message Not Public
Sign In & Join Server To View
so. how does this line look like if you try to use the name property. i cant help you propperly if i dont know what youre doing wrong
Unknown UserOP•4y ago
Message Not Public
Sign In & Join Server To View
Implement ToString on Pokemon, and then just writeline the pokemon
Unknown UserOP•4y ago
Message Not Public
Sign In & Join Server To View
The class
How to override the ToString method - C# Programming Guide
Learn how to override the ToString method in C#. Every class or struct inherits Object and gets ToString, which returns a string representation of that object.
i still dont know what you tried. if you post this maybe tag me. for now i dont have enough to help you.
Unknown UserOP•4y ago
Message Not Public
Sign In & Join Server To View
You do it in every class you want to have a good ToString in
Unknown UserOP•4y ago
Message Not Public
Sign In & Join Server To View
You implement it on your custom party type, and your pokemon type
Your party goes through its internal list and calls it on every pokemon
Unknown UserOP•4y ago
Message Not Public
Sign In & Join Server To View
Give it a try first
Unknown UserOP•4y ago
Message Not Public
Sign In & Join Server To View
Because you didn't implement ToString on Pokemon
Unknown UserOP•4y ago
Message Not Public
Sign In & Join Server To View
No
Orannis#3333
Your party goes through its internal list and calls it on every pokemon
Quoted by
<@!269674004065943552> from #how to Implement the IReadOnlyList interface to let other classes read from your internal list. (click here)
React with ❌ to remove this embed.
But start with just pokemon
Get that printing nicely
Then do the whole party
Unknown UserOP•4y ago
Message Not Public
Sign In & Join Server To View
Yep
Unknown UserOP•4y ago
Message Not Public
Sign In & Join Server To View
Do this
Unknown UserOP•4y ago
Message Not Public
Sign In & Join Server To View
Sure you do
Unknown UserOP•4y ago
Message Not Public
Sign In & Join Server To View
Show your code
Unknown UserOP•4y ago
Message Not Public
Sign In & Join Server To View
None of this is your
ToString
overrideUnknown UserOP•4y ago
Message Not Public
Sign In & Join Server To View
✅ This post has been marked as answered!