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 7488733
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T15:01:18+00:00 2026-05-29T15:01:18+00:00

I am creating a class in C# called Robot, and each robot requires a

  • 0

I am creating a class in C# called “Robot”, and each robot requires a unique ID property which gives themselves an identity.

Is there any way of creating an auto incremental ID for each new class object? So, If i created 5 new robots, their IDs respectively will be 1, 2, 3, 4, 5. If I then destroy robot 2 and create a new robot later, it will have the ID of 2. And if I add a 6th it will have the ID of 6 and so on..

Thanks.

  • 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-05-29T15:01:20+00:00Added an answer on May 29, 2026 at 3:01 pm

    This will do the trick, and operate in a nice threadsafe way. Of course it is up to you to dispose the robots yourself, etc. Obviously it won’t be efficient for a large number of robots, but there are tons of ways to deal with that.

      public class Robot : IDisposable
      {
        private static List<bool> UsedCounter = new List<bool>();
        private static object Lock = new object();
    
        public int ID { get; private set; }
    
        public Robot()
        {
    
          lock (Lock)
          {
            int nextIndex = GetAvailableIndex();
            if (nextIndex == -1)
            {
              nextIndex = UsedCounter.Count;
              UsedCounter.Add(true);
            }
    
            ID = nextIndex;
          }
        }
    
        public void Dispose()
        {
          lock (Lock)
          {
            UsedCounter[ID] = false;
          }
        }
    
    
        private int GetAvailableIndex()
        {
          for (int i = 0; i < UsedCounter.Count; i++)
          {
            if (UsedCounter[i] == false)
            {
              return i;
            }
          }
    
          // Nothing available.
          return -1;
        }
    

    And some test code for good measure.

    [Test]
    public void CanUseRobots()
    {
    
      Robot robot1 = new Robot();
      Robot robot2 = new Robot();
      Robot robot3 = new Robot();
    
      Assert.AreEqual(0, robot1.ID);
      Assert.AreEqual(1, robot2.ID);
      Assert.AreEqual(2, robot3.ID);
    
      int expected = robot2.ID;
      robot2.Dispose();
    
      Robot robot4 = new Robot();
      Assert.AreEqual(expected, robot4.ID);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a class called Projectiles which inherits from CCSprite class, Currently there are
I am creating a class called SelectionPage. Which essentially is a set of menues.
I just found myself creating a class called InstructionBuilderFactoryMapFactory. That's 4 pattern suffixes on
I am creating a class that determines which of number of registered WCF client
I'm working on creating a class file called MyClass - it works fine when
was thinking about creating a Single class called 'Request' to handle and clean POST
I'm just learning C++ and programming. I'm creating a class called Distance . I
I am creating a JavaScript class called ImageRotatorManager to manage a dynamically-loaded slideshow. When
Hi I'm creating a water scene and have a class called drawWater. The class
I've defined a class called Country that has a unique name field. class Country(models.Model):

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.