Hi there,
first, in the player class, if you want to expose player's skills as property:
private List<PlayerSkill> skills;
public ReadOnlyCollection<PlayerSkill> Skills {get; private set;}
If you used the List class for this purpose, while it wouldn't be possible to change its instance from outside, there would be still possibility to call Add on it, what you normally don't want until there is a good reason for it. That's why ReadOnlyCollection is used. It's a wrapper around List, skills with small s in this case, which you can see above it. It presents its content like a normal List, with only exception that there are no modifying methods, so outside caller can't mess it up.
Whenever you want to add skills to it, you just add items to underlying List and it will have immediate effect, because ReadOnlyCollection is just a wrapper around it as I said.
Then in Player's constructor (I'm omitting other parameters):
public Player(List<PlayerSkill> skills)
{
this.skills=skills;
Skills=new ReadOnlyCollection<PlayerSkill>(this.skills);
}
Although now there is still at least one reference to internal class field outside of it, because List is a reference datatype. If you want to completely prevent this, make a new copy like this:
public Player(List<PlayerSkill skills)
{
this.skills=new List<PlayerSkill>();
this.skills.AddRange(skills);
Skills=new ReadOnlyCollection<PlayerSkill>(this.skills);
}
Just for completeness, here is a preview of an AddSkill method
public void AddSkill(PlayerSkill skill)
{
skills.Add(skill);
}
Yeah, and don't forget to include System.Collections.ObjectModel namespace, so you have access to the ReadOnlyCollection.
You can find further reference at:
https://docs.microsoft.com/en-us/dotnet … mework-4.8
Best regards
Rastislav