如何将字符串添加到 string[] 数组?没有 .Add 功能 [英] How to add a string to a string[] array? There's no .Add function

查看:27
本文介绍了如何将字符串添加到 string[] 数组?没有 .Add 功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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 转换为字符串,然后将其添加到我的数组中.我该怎么做?

I'd like to convert the FI.Name to a string and then add it to my array. How can I do this?

推荐答案

您不能向数组添加项,因为它具有固定长度.您正在寻找的是一个 List,稍后可以使用 list.ToArray() 将其转换为数组,例如

You can't add items to an array, since it has fixed length. What you're looking for is a List<string>, which can later be turned to an array using list.ToArray(), e.g.

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

这篇关于如何将字符串添加到 string[] 数组?没有 .Add 功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆