Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7957331
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T04:14:04+00:00 2026-06-04T04:14:04+00:00

I have a class of 3 different linked lists (for saving the entities in

  • 0

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.

public class EntityBucket
{
    public LinkedList<IEntity> undeadEntities;
    public LinkedList<IEntity> objects;
    public LinkedList<IEntity> livingEntities;

    public EntityBucket()
    {
        undeadEntities = new LinkedList<IEntity>();
        objects = new LinkedList<IEntity>();
        livingEntities = new LinkedList<IEntity>();
    }

    public LinkedList<IEntity> GetList(IObject e)
    {
        return objects;
    }

    public LinkedList<IEntity> GetList(IUndead e)
    {
        return undeadEntities;
    }

    public LinkedList<IEntity> GetList(ILiving e)
    {
        return livingEntities;
    }

}

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:

public void Delete(IUndead e, World world)
{

     .....
     LinkedList<IEntity> list = buckets[k].GetList(e);
     .....
}

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.

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.

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T04:14:04+00:00Added an answer on June 4, 2026 at 4:14 am

    How about a better implementation to go with that better interface?

    public class EntityBucket
    {
      public LinkedList<IEntity> Entities;
    
      public IEnumerable<T> GetEntities<T>() where T : IEntity
      {
        return Entities.OfType<T>();
      }
    
    }
    
    
    List<IUndead> myBrainFinders = bucket.GetEntities<IUndead>().ToList();
    

    With this implementation, the caller better add each item to the right list(s). That was a requirement for your original implementation, so I figure it’s no problem.

    public class EntityBucket
    {
      Dictionary<Type, List<IEntity>> entities = new Dictionary<Type, List<IEntity>>();
    
      public void Add<T>(T item) where T : IEntity
      {
        Type tType = typeof(T);
        if (!entities.ContainsKey(tType))
        {
          entities.Add(tType, new List<IEntity>());
        }
        entities[tType].Add(item);
      }
    
      public List<T> GetList<T>() where T : IEntity
      {
        Type tType = typeof(T);
        if (!entities.ContainsKey(tType))
        {
          return new List<T>();
        }
        return entities[tType].Cast<T>().ToList();
      }
    
      public List<IEntity> GetAll()
      {
        return entities.SelectMany(kvp => kvp.Value)
          .Distinct() //to remove items added multiple times, or to multiple lists
          .ToList();
      }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two entities, Class and Student , linked in a many-to-many relationship. When
I have class that represents users. Users are divided into two groups with different
I have a large class, which I have divided into several different class extension
I have two different projects and in one, I have a class that defines
I have this class that has many class members, and a lot of different
I have one parent class, which is abstract class for now, for four different
I have a few different classes which origin is a another class. I have
I have created a button CSS class that uses background images with different positions
Let's say that I have a bunch of class instances that serve different purposes,
my model is basically a chain of objects linked by a foreign key: class

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.