2

I need to create an array of strings, easy enough... the only problem is that the strings are integers from 1-1000 and I really dont want to type each of them.

Can you create a loop that could create this?

right now it looks like this

        private readonly string[] _myArray = { "1", "2", "3", "4", "5" };

and its called by

        for (var i = 0; i < _myArray.Length; i++)
        {
            arFoo[i].SetBar(_myArray[i]);
        }

any suggestions on how to add the other 995 without manually typing them?

2
  • Are you generating random integers? Could you simply use the C# Random library to generate 1000 ints, convert them to strings, and then run the for loop? Commented May 20, 2011 at 21:02
  • I do not know if the readonly feature is important to you, but if it is, I see a lot of noise on the answers, as many of them disregard this detail Commented May 20, 2011 at 23:21

11 Answers 11

7

This is simple and clean:

readonly string[] _myArray 
    = Enumerable.Range(1, 1000)
        .Select(i => i.ToString())
        .ToArray();
Sign up to request clarification or add additional context in comments.

1 Comment

Nice and clean solution. Do You know how it compares in performance to 'normal' way of doing it - in for loop?
5

If you want to use LINQ:

    private readonly string[] _myArray;

    public Foo()
    {
        _myArray = Enumerable.Range(1, 1000).Select(s => s.ToString()).ToArray();
    }

Or more traditionally:

public class Foo
{
    private readonly string[] _myArray;

    public Foo()
    {
        _myArray = new string[1000];
        for(int i=1; i<=1000; i++)
        {
            _myArray[i - 1] = i.ToString();
        }
    }
}

Comments

2

How about

int NumberOfElements = 1000;
String[] Array = new String[NumberOfElements];

for(int i=0; i<Array.Length; i++)
{
   Array[i] = (i + 1).ToString();
}

2 Comments

Yours will do 0-999, not 1-1000.
Unless he needs the array to contain 1 in the first element. If so, this would probably work a little better: for(int i = 1; i<=NumberOfElements; i++ )
2

Do you need an array? You could just do this:

for (var i = 0; i < 1000; i++)       
{            
   arFoo[i].SetBar(i.ToString()); 
}

If you do need an array, understand that arrays in C# (and in .Net) are fixed-size. You would need another data structure, like a List<String> in order to add elements, then you can transform to an array (if truly needed) via ToArray().

Comments

2
var array = Enumerable.Range(1, 1000).Select(item => item.ToString()).ToArray();

Comments

2
int[] arr = Enumerable.Range(1, 1000).ToArray();

2 Comments

I was writing while @Anthony-Pegram posted! :(
This is not array of strings.
2
_myArray = new string[1000];
for (int i = 1; i <= 1000; i++) _myArray[i - 1] = i.ToString();

Comments

2
string[] arr = new string[1000];
for (int i = 1; i <= 1000; i++)
{
    arr[i-1] = i.ToString();
}

4 Comments

arr[i] should be arr[i-1]
Just change new int[1000] to new string[1000], and array indexes start from 0.
string[] arr = new int[1000]; doesn't even compile.
@Peri @vcsjones Originally I had read the question wrong and assumed it was integers he had wanted..I guess I must've missed out some parts when changing it. And the array indices were from a previous edit I missed.
1

You can simply do it in a for loop as follows, calling ToString on the int 'i'

private string[] _myArray = new string[1000];

for(int i=0;i<1000;i++)
{
  _myArray[i] = i.ToString();
}

Comments

1
private readonly string[] _myArray = new string[1000];

for (int i = 0; i < _myArray.Length; i++)
    _myArray[i] = i.ToString();

Comments

0

This is method that will generate array of string from 'from' to 'to' inclusive.

private string[] GetNumbers(int from, int to)
{
  int size = to - from + 1;
  string[] result = new string[size];
  int number = from;
  for (int i = 0; i < size; i++)
  {
    result[i] = number.ToString();
    number++;
  }
  return result;
}

string[] numbers = GetNumbers(1, 1000); // to get what You want

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.