代码参考:
using System; using System.Linq; namespace SliceDemo { class Program { static void Main(string[] args) { var myArray = new string[] { "10", "20", "30", "40", "50", "60", "70", "80", "90", "100" }; foreach (var e in myArray[0..5]) { Console.WriteLine(e); } Console.WriteLine("--------------------"); foreach (var e in myArray[^3..^1]) { Console.WriteLine(e); } Console.WriteLine("--------------------"); var q1 = myArray.Skip(2).Take(3); foreach (var e in q1) { if (e == "40") break; Console.WriteLine(e); } Console.WriteLine("--------------------"); foreach (var e in q1) { Console.WriteLine(e); } Console.WriteLine($"q1={string.Join(',', q1)}"); Console.WriteLine("--------------------"); var q2 = myArray[2..5]; foreach (var e in q2) { Console.WriteLine(e); } Console.WriteLine($"q2={string.Join(',',q2)}"); Console.WriteLine("--------------------"); var name = "张三"; var age = 23; var sex = "男"; var show = string.Format($"测试字符串拼接:我是{name},今年{age},家住在{{0}}", sex); Console.WriteLine(show); } } }
标签:q1,--------------------,Console,C#,切片,拼接,foreach,WriteLine,var From: https://www.cnblogs.com/exesoft/p/16866918.html