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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T05:52:08+00:00 2026-05-28T05:52:08+00:00

Hi there I have a bit of a noob question about creating unique variable

  • 0

Hi there I have a bit of a noob question about creating unique variable names in C#.
I would like to make an ArrayList of Enemy objects and I want to call each enemy “enemy[1]”
I would like to make a for loop and I would like to use the value of “i” to add on to the end of the word enemy.
so for instance:

forloop
{
Enemy enemy1 = new Enemy(actual parameters of the enemy class)
Enemy enemy2 = new Enemy();
Enemy enemy3 = new Enemy();
}

Any help would be appreciated.

  • 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-28T05:52:09+00:00Added an answer on May 28, 2026 at 5:52 am

    Variables cannot be dynamically named as you desire, but by using arrays or lists (but please don’t use ArrayLists, more below!), you can access via indexes.

    Create a List<Enemy> (List<T> is found in System.Collections.Generic)

    var enemies = new List<Enemy>();
    enemies.Add(new Enemy()); // add as many as you need
    

    Or if the collection does not need to be dynamically sized, you could use a simple array, of which there are several legal syntaxes for creation.

    Enemy[] enemies = { new Enemy(), new Enemy(), new Enemy() };
    

    Then you can write your loop and access via the index

    // enemies.Count if using List<Enemy>
    // enemies.Length if using Enemy[] array
    for (int index = 0; index < enemies.Count; index++) 
    {
        enemies[index].FireWeapon();
    }
    

    Or write a foreach loop

    foreach (Enemy enemy in enemies)
    {
        enemy.FireWeapon();
    }
    

    Your notion about using an ArrayList is outdated as of C# 2.0+ (the current release on the market is C# 4.0), or Visual Studio 2005 (2010 is the current market version). It is preferrable to use the strongly typed generic collections found in the aforementioned System.Collections.Generic namespace.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Ok this may seem like a bit of a noob question but one many
I have a bit of a design question if anyone at there cares to
Is there a way to have a 64 bit enum in C++? Whilst refactoring
What alternatives are there to GAE, given that I already have a good bit
My problem is a little bit hard but simple. I have (or there is)
Work on asp.net vs05. I have three type of value Like:IsDesign,IsPrinting,IsInstall they are bit
There have been some questions about whether or not JavaScript is an object-oriented language.
There have been several questions already posted with specific questions about dependency injection ,
There have been several questions recently about database indexing and clustered indexing and it
This is a bit of noob question - I'm still fairly new to C#

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.