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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T12:03:39+00:00 2026-06-05T12:03:39+00:00

I have an array of strings that I am looping through. I would like

  • 0

I have an array of strings that I am looping through. I would like to loop through the array and on each iteration, create a new object with a name that matches the string value.

For example;

string[] array = new string[] { "one", "two", "three" };

class myClass(){

    public myClass(){
    }
}

foreach (string name in array)
{
   myClass *value of name here* = new myClass(); 
}

Would result in three objects being instantiated, with the names “one”, “two” and “three”.

Is this possible or is there are better solution?

  • 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-05T12:03:41+00:00Added an answer on June 5, 2026 at 12:03 pm

    What are you trying to do is not possible in statically-typed language. IIRC, that’s possible on PHP, and it’s not advisable though.

    Use dictionary instead: http://ideone.com/vChWD

    using System;
    using System.Collections.Generic;
    
    class myClass{
    
        public string Name { get; set; }
        public myClass(){
        }
    }
    
    class MainClass
    {
    
        public static void Main() 
        {
            string[] array = new string[] { "one", "two", "three" };
            IDictionary<string,myClass> col= new Dictionary<string,myClass>();
            foreach (string name in array)
            {
                  col[name] = new myClass { Name = "hahah " + name  + "!"};
            }
    
            foreach(var x in col.Values)
            {
                  Console.WriteLine(x.Name);
            }
    
            Console.WriteLine("Test");
            Console.WriteLine(col["two"].Name);
        }
    }
    

    Output:

    hahah one!
    hahah two!
    hahah three!
    Test
    hahah two!
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a For Each loop that is looping through an array of strings
I have a for each loop that is looping through an array of data
I have a .plist file that looks like this: <plist version=1.0> <array> <dict> <key>name</key>
I have an array of Place objects. Each Place object has a name and
I have an array of strings that I want to use for button titles
I have an array of input strings that contains either email addresses or account
I have a variable that contains : Sometimes array of strings and sometimes array
I have an array of strings var controlsToGet = new[] {lblHome,lblContact}; I have List<LanguageControl>
I'm trying to create an array, looping through another array and taking the string
I have an array of fields (not rows!) I want to loop through, in

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.