Share via

Append values to existing array

Padmanabhan, Venkatesh 125 Reputation points
2023-04-07T05:33:47.16+00:00

Hi. I am looping through the contents using a for each loop and storing the value in string array. Even though the loop is going more than thrice, only the last loop data is getting stored in the string array. How to store all the values in the array ? Below is the code which is used :

 string[] values = new string[] { };
                var applicationSettings = ConfigurationManager.GetSection(ddlAppln.SelectedValue.ToString().Trim()) as NameValueCollection; 

                if (applicationSettings.Count == 0)
                {
                    Console.WriteLine("Application Settings are not defined");
                }
                else
                {
                    foreach (var key in applicationSettings.AllKeys)
                    {                        
                        values =  applicationSettings[key].ToString().Split(',');
                    }
                }
Developer technologies | C#
Developer technologies | C#

An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.

0 comments No comments

Answer accepted by question author
  1. Jiachen Li-MSFT 34,241 Reputation points Microsoft External Staff
    2023-04-07T06:44:21.68+00:00

    Hi, welcome to Microsoft Q&A.

    You can use append method to add each string to values.

    Check the following code.

                string[] values = new string[] { };
                var applicationSettings = ConfigurationManager.GetSection(ddlAppln.SelectedValue.ToString().Trim()) as NameValueCollection;
    
                if (applicationSettings.Count == 0)
                {
                    Console.WriteLine("Application Settings are not defined");
                }
                else
                {
                    foreach (var key in applicationSettings.AllKeys)
                    {
                        foreach(string str in applicationSettings[key].ToString().Split(','))
                        {
                            values = values.Append<string>(str).ToArray();
                        }
                        
                    }
                }
    

    Best Regards.

    Jiachen Li


    If the answer is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    2 people found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.