Question: I'm working on a networked game. In this game, the base entity is called an actor. An actor can either be replicated or local, with each instance having a "replicates" field that represents this.

When managing a scene, a collection of actor must be maintained. My problem is finding a suitable solution to this. Right now I store a collection of references to local actors, replicated actors and all actors. This means data is duplicated which results in code like this:

```
        for( int i = 0; i < actorsToRemove.Count; ++i )
        {
            if( actorsToRemove[i].Replicates )
            {
                replicatedActors.Remove( actorsToRemove[i].Id );
            }
            else
            {
                localActors.Remove( actorsToRemove[i] );
            }
            allActors.Remove( actorsToRemove[i] );
        }
        actorsToRemove.Clear();

```

Instead of just removing from a single collection. 

I could have one single collection that holds all actors and use properties that return another collection. For example:

```
public List<Actor> ReplicatedActors
{
    get
    {
        // loop through all actors and return a new list that holds all replicating actors.
    }
}

```

But this seems like too much unnecessary copying.

What would be the best way to go about this?

Answer: 
What you suggested is absolutely fine. There isn't any unnecessary copying. Although, to make things simpler, I suggest using LINQ extension methods for filtering.

```
using System.Linq;

public IEnumerable<Actor> ReplicatedActors
{
    get { return allActors.Where(actor => actor.Replicates); }
}

public IEnumerable<Actor> LocalActors
{
    get { return allActors.Where(actor => !actor.Replicates); }
}

```

You'll still have to remove actors from `allActors`, these two are separate read-only lists of actors derived from the list of all actors.

Edit: I also suggest changing your actor removal loop to be a `foreach` loop for simplicity.

```
foreach (var actor in actorsToRemove)
{
    allActors.Remove(actor);
}
actorsToRemove.Clear();

```
If you have three lists (localActors, replicatedActors, allActors) and you want to remove from them elements from actorsToRemove (with corresponding flag), you can utilise `.Except` method:

```
localActors = localActors.Except(actorsToRemove.Where(a => !a.Replicates).ToList();
replicatedActors = replicatedActors.Except(actorsToRemove.Where(a => a.Replicates).ToList();
allActors = allActors.Except(actorsToRemove).ToList();
actorsToRemove.Clear();

```

The solution with properties doesn't do exactly that. Using properties has the advantage that you can avoid storing data in three separate collections. It'll require using those properties inside a single class, and will not work for random lists.

Edit: My original idea with `.RemoveRange` was totally wrong, sorry. `.Except` should work with any `IEnumerable<>` though. It looks a bit noisy to my liking though :(.