我有一个清单:
var x = new List<string>(){"a","b","c"}
我正在寻找一个非常简单的方法来改变一个例子后的所有项目:
var x = new List<string>(){"a","b","c"}
var y = new List<string>(){"d","e","f"}
x.addAfterFirst(y);
Run Code Online (Sandbox Code Playgroud)
结果 x= "a","d","e","f"
我知道'x.Skip(1)'可以回复我的信息.我需要设置它.
您可以使用Take Extension方法获取前n个 项目,x并y使用Concat扩展方法将它们连接起来:
List<string> x = new List<string> { "a", "b", "c" };
List<string> y = new List<string> { "d", "e", "f" };
int n = 1;
List<string> result = x.Take(n).Concat(y).ToList();
// result == { "a", "d", "e", "f" }
Run Code Online (Sandbox Code Playgroud)
如果要x在原地修改而不是创建新列表,可以使用RemoveRange方法删除前n个项后面的所有项,并将AddRange方法附加y到x:
List<string> x = new List<string> { "a", "b", "c" };
List<string> y = new List<string> { "d", "e", "f" };
int n = 1;
x.RemoveRange(n, x.Count - n);
x.AddRange(y);
// x == { "a", "d", "e", "f" }
Run Code Online (Sandbox Code Playgroud)