c# - C#-添加到字符串[]成员

标签 c# asp.net linq visual-studio

我有一个名为lst的List

[0] "ABC" "DEF" "GHI"
[1] "JKL" "MNO" "PQR"
[2] etc, etc...


如何在每个第一个成员的末尾添加另一个字符串?

字符串s =“ EndOfBlock”;

[0] "ABC" "DEF" "GHI" "EndOfBlock"
[1] "JKL" "MNO" "PQR" "EndOfBlock"
[2] etc, etc...


谢谢。

最佳答案

正如评论者所指出的,听起来您确实想要List<List<string>>。但是,如果您想坚持使用List<string[]>,这就是我扩展每个数组的方式。

List<string[]> list = ...
for(int i = 0; i < list.Count; i++)
{
    // copy to a local variable (list[i] is a property, which can't be passed 'ref')
    string[] array = list[i];

    // resize the local variable. (this creates a new string[] object)
    Array.Resize(ref array, array.Length + 1);
    array[array.Length - 1] = "EndOfBlock";

    // put the new object back in the list, replacing the old smaller one.
    list[i] = array;
}

关于c# - C#-添加到字符串[]成员,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12587266/

相关文章:

c# - Linq To XML、yield 等

c# - Datagrid 在滚动后选择了错误的行

c# - 从 Exchange Web 服务基本身份验证迁移到 OAuth 2.0

asp.net - 如何从aspx页面调用代码隐藏方法?

c# - For vs. Linq - 性能 vs. future

c# - 从列表值动态创建匿名对象c#

c# - 多语言应用程序工具包填充 xlf 文件

c# - 如何用堆栈添加大数?

c# - uri 相对与绝对

javascript - 如何使用asp.net core razor Pages在CKEditor 5中上传图像