So, I created a list:

protected Dictionary<Type, List<Entity>> entities;

The Entities shown above inherit multiple interfaces such as Clickable, Hoverable and Focusable.

My question is iterating over them, I need a method that allows quick rendering of the entire list i.e:

foreach (KeyValuePair<Type, List<Entity>> entry in entities)
{
    foreach (Entity e in entry.Value)
    {
        e.Render(delta, drawingContext);
    }
}

and also allows for iterating over the individual inherited types:

 foreach (Clickable c in entities[typeof(Clickable)])
 {
     ... Perform click
 }

For my question: does "typeof" include the types that are inherting clickable? e.g:

 abstract class ClickableEntity : Entity, Clickable

My issue is efficiency and expandability, I understand that I could just use multiple lists but this approach allows me to understand the inner-workings of c#. Is it efficient? Big-O maybe?

有帮助吗?

解决方案

does "typeof" include the types that are inherting clickable?

No, it doesn't. ClickableEntity and Clickable are two different types, so they are different keys in the dictionary.

Instead of a dictionary, you could just use a single List<Entity>, and filter it using OfType:

 protected List<Entity> entities;

 foreach (var c in entities.OfType<Clickable>())
 {
     ... Perform click
 }
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top