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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T11:56:54+00:00 2026-06-11T11:56:54+00:00

I am using C# to create an app and want to be able to

  • 0

I am using C# to create an app and want to be able to easily do a foreach() loop through my 120 strings that i have. All these strings are constructed as below:

public class _currentweekapi
    {
        public string _1_artist { get; set; }
        public string _2_artist { get; set; }
        public string _3_artist { get; set; }
        ...
    }

Is it possible to be able to do a foreach() loop to loop through all the values. The strings are set automatically from a JSON feed so i cannot get the values straight into a list.

I have read up on the topic and understand there are ways through ‘Reflection’ but I am new to C# and reading up on it doesn’t make any sense to me.

Could someone please help me with a solution to this stating exactly how I could implement this?

  • 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-11T11:56:55+00:00Added an answer on June 11, 2026 at 11:56 am

    The first thing you should be trying to do, is NOT store the strings in explicitly named properties, as your class definition appears to be doing. The most appropriate scenario would seem to be a List<string> type property that can hold them all in your class. This would also mean you already have your list ready to go.
    So, if you are able to, change the class, or use another, which accepts the JSON feed, and uses .Add() on a property of type List<string>, rather than explicitly setting 120 properties.

    Like this:

    public class ArtistData
    {
        public List<string> Artists{ get; set; }
    
        public ArtistData()
        {
            this.Artists = new List<string>(0);
        }
    
        public void PopulateArtists(string jsonFeed)
        {
            // here something desrializes your JSON, allowing you to extract the artists...
            // here you would be populating the Artists property by adding them to the list in a loop...
        }
    }
    

    Then, you have your list in the property Artists, and can use the list directly, or return it by doing:

    string[] artists = myInstance.Artists.ToArray();
    

    However, you seem to have indicated that you cannot change the fact that they end up served to you as individual properties in the class you showed us, so…

    Assuming you have no choice, but to start from a class such as the one you showed, here is a way you could do a loop through all those values, just as you asked for, all that will be required is that you pass your class instance into one of the methods below as the one parameter that each requires:

        // this will return a list of the values contained in the properties...
        public List<string> GetListFromProperties<T>(T instance)
        {
            Type t = typeof(T);
            PropertyInfo[] props = t.GetProperties(BindingFlags.Public | BindingFlags.Instance);
    
            // As a simple list...
            List<string> artists = new List<string>(props.Length);
            for (int i = 0; i < props.Length; i++)
            {
                if(!props[i].Name.Contains("_artist")){ continue; }
                artists.Add(props[i].GetValue(instance, null).ToString());
            }
    
            return artists;
        }
    
        // this will return a dictionary which has each artist stored
        // under a key which is the name of the property the artist was in.
        public Dictionary<string,string> GetDictionaryFromProperties<T>(T instance)
        {
            Type t = typeof(T);
            PropertyInfo[] props = t.GetProperties(BindingFlags.Public | BindingFlags.Instance);
    
            // As a dictionary...
            Dictionary<string,string> artists = new Dictionary<string,string>(props.Length);
            for (int i = 0; i < props.Length; i++)
            {
                if(artists.ContainsKey(props[i].Name) || !props[i].Name.Contains("_artist")){ continue; }
                artists.Add(props[i].Name, props[i].GetValue(instance, null).ToString());
            }
    
            return artists;
        }
    

    Either one should help, but do not use the dictionary one, as it entails more overhead, unless you really need to know the name of the property each artist came from as well, in which case it is more helpful than a simple list.

    Incidentally, since the methods are generic, that is, they accept a parameter of type T, the same methods will work for ANY class instance, not just the one you are struggling with now.

    REMEMBER: although it may appear extremely convenient to you at the moment, this is not necessarily the best way to approach this. Better than this, would be the initial suggestions I made for re-working the class altogether so this sort of thing is not required.

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

Sidebar

Related Questions

I have a single model in my django app that I want to create
I want to create an app which plays videos using the RTP protocol. I
So basically I want to create a GUI app using PySide and the Qt
i want to create a chat app using flex 4.5 mobile project for android
I want to create a simple login functionality in WP7 app using remote MySQL
I'm using FMDB for my iPhone App database and i want to create the
I am creating a desktop app that will create some reports. I want to
I want to be able to quickly create some simple ASP.NET reports that don't
I have a project where I need to create a desktop app that acts
I have a process that creates a PDF. I want these PDF's to be

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.