private string[] ColeccionDeCortes(string Path)
{
    DirectoryInfo X = new DirectoryInfo(Path);
    FileInfo[] listaDeArchivos = X.GetFiles();
    string[] Coleccion;

    foreach (FileInfo FI in listaDeArchivos)
    {
        //Add the FI.Name to the Coleccion[] array, 
    }

    return Coleccion;
}

我想转换FI。Name到字符串中,然后将其添加到数组中。我该怎么做呢?


当前回答

使用List<T> from System.Collections.Generic

List<string> myCollection = new List<string>();

…

myCollection.Add(aString);

或者,简写(使用集合初始化器):

List<string> myCollection = new List<string> {aString, bString}

如果您确实希望在结尾使用数组,请使用

myCollection.ToArray();

最好是将其抽象到接口,比如IEnumerable,然后直接返回集合。

编辑:如果你必须使用一个数组,你可以预先分配它到正确的大小(即FileInfo的数量)。然后,在foreach循环中,为接下来需要更新的数组索引维护一个计数器。

private string[] ColeccionDeCortes(string Path)
{
    DirectoryInfo X = new DirectoryInfo(Path);
    FileInfo[] listaDeArchivos = X.GetFiles();
    string[] Coleccion = new string[listaDeArchivos.Length];
    int i = 0;

    foreach (FileInfo FI in listaDeArchivos)
    {
        Coleccion[i++] = FI.Name;
        //Add the FI.Name to the Coleccion[] array, 
    }

    return Coleccion;
}

其他回答

使用List<T> from System.Collections.Generic

List<string> myCollection = new List<string>();

…

myCollection.Add(aString);

或者,简写(使用集合初始化器):

List<string> myCollection = new List<string> {aString, bString}

如果您确实希望在结尾使用数组,请使用

myCollection.ToArray();

最好是将其抽象到接口,比如IEnumerable,然后直接返回集合。

编辑:如果你必须使用一个数组,你可以预先分配它到正确的大小(即FileInfo的数量)。然后,在foreach循环中,为接下来需要更新的数组索引维护一个计数器。

private string[] ColeccionDeCortes(string Path)
{
    DirectoryInfo X = new DirectoryInfo(Path);
    FileInfo[] listaDeArchivos = X.GetFiles();
    string[] Coleccion = new string[listaDeArchivos.Length];
    int i = 0;

    foreach (FileInfo FI in listaDeArchivos)
    {
        Coleccion[i++] = FI.Name;
        //Add the FI.Name to the Coleccion[] array, 
    }

    return Coleccion;
}

为什么不用for循环代替foreach循环呢?在这种情况下,您无法获得foreach循环当前迭代的索引。

文件名可以这样添加到字符串[]中,

private string[] ColeccionDeCortes(string Path)
{
  DirectoryInfo X = new DirectoryInfo(Path);
  FileInfo[] listaDeArchivos = X.GetFiles();
  string[] Coleccion=new string[listaDeArchivos.Length];

  for (int i = 0; i < listaDeArchivos.Length; i++)
  {
     Coleccion[i] = listaDeArchivos[i].Name;
  }

  return Coleccion;
}
string[] MyArray = new string[] { "A", "B" };
MyArray = new List<string>(MyArray) { "C" }.ToArray();
//MyArray = ["A", "B", "C"]

使用System.Linq添加对Linq的引用;并使用提供的扩展方法追加:公共静态IEnumerable<TSource>追加<TSource>(此IEnumerable<TSource>源,TSource元素) 然后你需要使用. toarray()方法将它转换回字符串[]。

这是可能的,因为类型string[]实现了IEnumerable,它也实现了以下接口:IEnumerable<char>, IEnumerable, IComparable, IComparable< string >, IConvertible, IEquatable< string >, ICloneable

using System.Linq;
public string[] descriptionSet new string[] {"yay"};
descriptionSet = descriptionSet.Append("hooray!").ToArray(); 

请记住,ToArray分配新的数组,因此,如果你添加更多的元素,你不知道你将有多少,最好使用List from System.Collections.Generic。

因为数组的长度是固定的,所以不能向数组中添加项。您要查找的是List<string>,稍后可以使用List . toarray()将其转换为数组,例如。

List<string> list = new List<string>();
list.Add("Hi");
String[] str = list.ToArray();