Dynamically create an array of strings in C#

To dynamically create a string array, do not use string[].  Rather use List <T> as shown in this example:

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace TestStringArray
{
    class Program
    {
        static void Main(string[] args)
        {
            List<String> arString = new List<string>();

            for (int i=0; i<10; i++)
            {
                int iProduct = i * i;
                arString.Add("Test " + iProduct.ToString());

            }

            foreach (string str in arString)
            {
                Console.WriteLine("String: " + str);
            }

            Console.ReadLine();
        }
    }
}
Advertisements

One Response to Dynamically create an array of strings in C#

  1. goedkoop verhuisbedrijf…

    […]Dynamically create an array of strings in C# « Jim Lahman's Weblog[…]…

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: