1

I have a loop that is generating about 150 unique strings. How can I add these strings to an array?

Here is my loop:

for (int intCounter = 0; intCounter < projectCount; intCounter ++)
{
    var projectname = project.value[intCounter].name;
    var releaseUri = "http://tfs1:8080/tfs/defaultcollection/" + projectname + "/_apis/release/releases?api-version=3.0-preview.2&searchText=911&minCreatedTime=" + date + "T00:00:00.00Z";
    Console.WriteLine(releaseUri);
}

The Console.WriteLine(releaseUri) prints each url. but I would like to store the releaseUri in an array.

1
  • myArray[intCounter] = releaseUri; Commented Feb 5, 2019 at 20:23

5 Answers 5

11

Lists are normally better than arrays.

    var releaseUris = new List<string>();
    foreach(var project in projects)
    {
        var releaseUri = $"http://tfs1:8080/tfs/defaultcollection/" + project.projectname + "/_apis/release/releases?api-version=3.0-preview.2&searchText=911&minCreatedTime=" + project.date + "T00:00:00.00Z";
        releaseUris.Add(releaseUri);
    }
Sign up to request clarification or add additional context in comments.

2 Comments

Huh, rly? Initialization new instance of List without parameters creates an object to store 4 elements. When you add the fifth element List will allocate new array to store 8 elements and then when you add the 9-th it will alloacte another to store 16 and so on. It's not normally better if you know array length and not gonna to change array after initialization
Just as a side note, you can pass in the size to the List constructor: new List<string>(projectCount)
1

@ShaneP,

You will need to declare an array outside of the for loop like so.

string[] releaseUriArray = new string[projectCount];

for (int intCounter = 0; intCounter < projectCount; intCounter ++)
{
    var projectname = project.value[intCounter].name;
    var releaseUri = "http://tfs1:8080/tfs/defaultcollection/" + projectname + "/_apis/release/releases?api-version=3.0-preview.2&searchText=911&minCreatedTime=" + date + "T00:00:00.00Z";
    // Here you are adding the releaseUri strings to the releaseUriArray
    releaseUriArray[intCounter] = releaseUri;

} 

// print your uris from the array here
for (int intCounter = 0; intCounter < projectCount; intCounter ++)
{
    var releaseUri = releaseUriArray[intCounter];
    Console.WriteLine(releaseUri);
} 

Comments

0

You can use an array in this case as you know the number of elements. Initialize it and set the items as you go

var arr = new string[projectCount];
for (int intCounter = 0; intCounter < projectCount; intCounter ++)
{
    var projectname = project.value[intCounter].name;
    var releaseUri = "http://tfs1:8080/tfs/defaultcollection/" + projectname + "/_apis/release/releases?api-version=3.0-preview.2&searchText=911&minCreatedTime=" + date + "T00:00:00.00Z";
    Console.WriteLine(releaseUri);
    arr[intCounter] = releaseUri;
}

Comments

0

If you now projectCount then you can create an array with needed element numbers and just set its items by index.

var urls = new string[projectCount];
for (int intCounter = 0; intCounter < projectCount; intCounter ++)
{
    var projectname = project.value[intCounter].name;
    var releaseUri = "http://tfs1:8080/tfs/defaultcollection/" + projectname + "/_apis/release/releases?api-version=3.0-preview.2&searchText=911&minCreatedTime=" + date + "T00:00:00.00Z";
    urls[i] = releaseUri;
}

Or you can just use an dynamic array and add elements using Add() method to be able to change array size after initialization.

var urls = new List<string>();
for (int intCounter = 0; intCounter < projectCount; intCounter ++)
{
    var projectname = project.value[intCounter].name;
    var releaseUri = "http://tfs1:8080/tfs/defaultcollection/" + projectname + "/_apis/release/releases?api-version=3.0-preview.2&searchText=911&minCreatedTime=" + date + "T00:00:00.00Z";
    urls.Add(releaseUri);
}

Also you no need to use loops and can solve your problem with just 1 string of code using LINQ:

var urls = project
              .value
              .Select(p => "http://tfs1:8080/tfs/defaultcollection/" + p.projectname + "/_apis/release/releases?api-version=3.0-preview.2&searchText=911&minCreatedTime=" + date + "T00:00:00.00Z")
              .ToArray();

Comments

0

One easy way to do this would be to create a template Uri that has placeholders for project name and date (using {0} and {1} in the string), then with some Linq extension methods (Select and ToList) and string.Format, you can and generate your strings from an Enumerable.Range:

// Project name and date will be inserted where {0} and {1} are below
string uriTemplate = "http://tfs1:8080/tfs/defaultcollection/{0}/_apis/release/" +
    "releases?api-version=3.0-preview.2&searchText=911&minCreatedTime={1}T00:00:00.00Z";

string[] releaseUris = Enumerable.Range(0, projectCount)
    .Select(i => string.Format(uriTemplate, project.value[i], date))
    .ToArray();

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.