Edw*_*uay 5 c# arrays string join
什么是String.Eoin非字符串数组的简写方法,如第二个示例中所示?
string[] names = { "Joe", "Roger", "John" };
Console.WriteLine("the names are {0}", String.Join(", ", names)); //ok
decimal[] prices = { 39.99M, 29.99m, 29.99m, 19.99m, 49.99m };
Console.WriteLine("the prices are {0}", String.Join(", ", prices)); //bad overload
Run Code Online (Sandbox Code Playgroud)
cjk*_*cjk 13
如果你有LINQ:
decimal[] prices = { 39.99M, 29.99m, 29.99m, 19.99m, 49.99m };
Console.WriteLine("the prices are {0}",
String.Join(", ",
prices.Select(p => p.ToString()).ToArray()
)
);
Run Code Online (Sandbox Code Playgroud)
Console.WriteLine("the prices are {0}", String.Join(", ", Array.ConvertAll(prices, p => p.ToString()));
Run Code Online (Sandbox Code Playgroud)