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.
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);
}
@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);
}
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;
}
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();
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();
A List would be better because you don't know exactly how many items you will be handling. It would look something like this List<String> myList = new List<String>();
Then simply myList.Add(releaseURi);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.