C#如何将项目添加到数组

时间:2019-02-05 20:20:26

标签: c# arrays for-loop

我有一个循环,它生成约150个唯一的字符串。如何将这些字符串添加到数组? 这是我的循环:

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);
}

Console.WriteLine(releaseUri)打印每个URL。但我想将releaseUri存储在数组中。

6 个答案:

答案 0 :(得分:9)

列表通常比数组更好。

    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);
    }

答案 1 :(得分:1)

@ShaneP,

您将需要像这样在for循环之外声明一个数组。

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);
} 

答案 2 :(得分:0)

在这种情况下,您可以使用数组,因为您知道元素的数量。对其进行初始化并随需设置项目

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;
}

答案 3 :(得分:0)

如果您现在projectCount,则可以创建具有所需元素编号的数组,只需按索引设置其项目即可。

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;
}

或者您可以只使用dynamic array并使用Add()方法添加元素,以便在初始化后更改数组大小。

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);
}

您也无需使用循环,并且可以使用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();

答案 4 :(得分:0)

一种简单的方法是创建一个模板Uri,该模板具有项目名称和日期的占位符(在字符串中使用{0}{1}),然后添加一些Linq扩展方法(SelectToList)和string.Format,您可以从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();

答案 5 :(得分:-1)

列表会更好,因为您不知道要处理多少个项目。看起来像这样 List<String> myList = new List<String>(); 然后简单地 myList.Add(releaseURi);