Note that there are some explanatory texts on larger screens.

plurals
  1. POC#, generic way to access different lists within a class
    text
    copied!<p>I have a class of 3 different linked lists (for saving the entities in a game I'm working on). The lists are all of objects with the same base type, but I keep them separate for processing reasons. Note that IEntity, IObject and IUndead all inherited from IEntity.</p> <pre><code>public class EntityBucket { public LinkedList&lt;IEntity&gt; undeadEntities; public LinkedList&lt;IEntity&gt; objects; public LinkedList&lt;IEntity&gt; livingEntities; public EntityBucket() { undeadEntities = new LinkedList&lt;IEntity&gt;(); objects = new LinkedList&lt;IEntity&gt;(); livingEntities = new LinkedList&lt;IEntity&gt;(); } public LinkedList&lt;IEntity&gt; GetList(IObject e) { return objects; } public LinkedList&lt;IEntity&gt; GetList(IUndead e) { return undeadEntities; } public LinkedList&lt;IEntity&gt; GetList(ILiving e) { return livingEntities; } } </code></pre> <p>I have 3 methods for retrieving each of the lists, currently based on their parameters. The fact that there are 3 is fine, since I know each list will in some way or another require its own accessor. Passing an instantiated object is not ideal though, as I may want to retrieve a list somewhere without having an object of similar type at hand. Note that the object here is not even used in the GetList methods, they are only there to determine which version to use. Here is an example where I have an instantiated object at hand:</p> <pre><code>public void Delete(IUndead e, World world) { ..... LinkedList&lt;IEntity&gt; list = buckets[k].GetList(e); ..... } </code></pre> <p>I don't like this current implementation as I may not always have an instantiated object at hand (when rendering the entities for example). I was thinking of doing it generically but I'm not sure if this is possible with what I want to do. With this I also need 3 Delete methods (and 3 of any other, such as add and so forth) - one for each type, IUndead, IObject and ILiving. I just feel that this is not the right way of doing it.</p> <p>I'll post what I have tried to do so far on request, but my generics is rather bad and I feel that it would be a waste for anyone to read this as well.</p> <p>Finally, performance is very important. I'm not prematurely optimizing, I am post-optimizing as I have working code already, but need it to go faster. The getlist methods will be called very often and I want to avoid any explicit type checking.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload