c# 长行代码格式化
c# long line of code formatting
这行代码是我写的。这是不可读的。有没有巧妙的方法将其分解为多行代码,长度小于 80 或 100 个字符?
Console.WriteLine(String.Join("\n", testResults.Select(row => String.Join("|", row.Select(column => String.Format("{0,20}", column.ToString()))))));
是的.. 将 row.Select 提取到上面一行的变量中并使用它。同样提取testResults.Select。
如果您不想拆分作业,这应该是最短的方法。但是我建议分开看,这样读起来更好。
Console.WriteLine(String.Join("\n",
testResults.Select(
row => String.Join("|", row.Select(column => String.Format("{0,20}", column.ToString()))))));
我建议将 query 本身和它的最终 representation(控制台输出)分开:
// Query: what to output
var testReport = testResults
.Select(row => String.Join("|", row
.Select(column => String.Format("{0,20}", column)))); // .ToString() is redundant
// Representation: how to output (print on the console in one go)
Console.WriteLine(String.Join(Environment.NewLine, testReport));
这行代码是我写的。这是不可读的。有没有巧妙的方法将其分解为多行代码,长度小于 80 或 100 个字符?
Console.WriteLine(String.Join("\n", testResults.Select(row => String.Join("|", row.Select(column => String.Format("{0,20}", column.ToString()))))));
是的.. 将 row.Select 提取到上面一行的变量中并使用它。同样提取testResults.Select。
如果您不想拆分作业,这应该是最短的方法。但是我建议分开看,这样读起来更好。
Console.WriteLine(String.Join("\n",
testResults.Select(
row => String.Join("|", row.Select(column => String.Format("{0,20}", column.ToString()))))));
我建议将 query 本身和它的最终 representation(控制台输出)分开:
// Query: what to output
var testReport = testResults
.Select(row => String.Join("|", row
.Select(column => String.Format("{0,20}", column)))); // .ToString() is redundant
// Representation: how to output (print on the console in one go)
Console.WriteLine(String.Join(Environment.NewLine, testReport));