ASP.NET 将项目添加到数组

use*_*331 -3 c# asp.net

我定义了这个数组:

string[] emailAddress = {};
Run Code Online (Sandbox Code Playgroud)

我想做的是将项目添加到这个数组中,如下所示:

emailAddress[] = de.Properties["mail"][0].ToString();
Run Code Online (Sandbox Code Playgroud)

我收到无法将字符串转换为数组错误。如何将项目添加到数组中?

Igo*_*gor 5

string[] emailAddress = new string[1]; // initialize it to a length of 1
emailAddress[0] = de.Properties["mail"][0].ToString(); // assign the string to position 1
Run Code Online (Sandbox Code Playgroud)

如果您在运行时不知道长度,则使用通用列表并随后进行转换。

var emailAddress = new List<string>();
emailAddress.Add(de.Properties["mail"][0].ToString());
var myArray = emailAddress.ToArray(); // create an array from the list
Run Code Online (Sandbox Code Playgroud)

我建议您阅读这篇有关如何在 C# 中使用数组的文章(或其他一些教程)。 https://msdn.microsoft.com/en-us/library/aa288453(v=vs.71).aspx