有没有一种方法可以在 C# 中将内插字符串拆分为多行,同时在 运行 时间执行相同的性能
Is there a way to split interpolated strings over multiple lines in C# whilst executing the same at run time in terms of performance
我一直在与同事讨论格式化以下代码的最佳方式。
return $" this is a really long string.{a} this is a really long string. this is a really long string. this is a really long string. this is a really long string. this is a really long string. this is a really long string. this is a really long string. this is a really long string. this is a really long string. this is a really long string. this is a really long string.{b} this is a really long string. this is a really long string. this is a really long string. this is a really long string. this is a really long string. this is a really long string.{c}";
我的目标是:(在预先指定的点换行,即大约 80 个字符)
return $" this is a really long string.{a} this is a really long string. this is a really long string." +
$" this is a really long string. this is a really long string. this is a really long string." +
$" this is a really long string. this is a really long string. this is a really long string." +
$" this is a really long string. this is a really long string. this is a really long string." +
$"{b} this is a really long string. this is a really long string. this is a really long string." +
$" this is a really long string. this is a really long string. this is a really long string.{c}";
但是我担心我在运行时添加了不必要的工作。是这样吗?
如果有,有更好的方法吗?
另外我觉得换行不是一个好的答案><
TLDR String.Format 被调用进行插值,因此连接被插值的字符串意味着对 String.Format
的更多调用
我们来看看IL
当你有这些问题时,为了更好地了解实际发生了什么,最好查看 IL(中间语言),这是你的代码被编译成然后 运行 .NET 运行时间。您可以使用 ildasm 检查已编译的 .NET DLL 和 EXE 的 IL。
连接多个字符串
所以在这里你可以看到在幕后,每个连接的字符串都被调用了 String.Format。
使用一个长字符串
在这里你看到字符串格式只被调用一次,这意味着如果你以这种方式谈论性能会稍微好一些。
我一直在与同事讨论格式化以下代码的最佳方式。
return $" this is a really long string.{a} this is a really long string. this is a really long string. this is a really long string. this is a really long string. this is a really long string. this is a really long string. this is a really long string. this is a really long string. this is a really long string. this is a really long string. this is a really long string.{b} this is a really long string. this is a really long string. this is a really long string. this is a really long string. this is a really long string. this is a really long string.{c}";
我的目标是:(在预先指定的点换行,即大约 80 个字符)
return $" this is a really long string.{a} this is a really long string. this is a really long string." +
$" this is a really long string. this is a really long string. this is a really long string." +
$" this is a really long string. this is a really long string. this is a really long string." +
$" this is a really long string. this is a really long string. this is a really long string." +
$"{b} this is a really long string. this is a really long string. this is a really long string." +
$" this is a really long string. this is a really long string. this is a really long string.{c}";
但是我担心我在运行时添加了不必要的工作。是这样吗? 如果有,有更好的方法吗?
另外我觉得换行不是一个好的答案><
TLDR String.Format 被调用进行插值,因此连接被插值的字符串意味着对 String.Format
的更多调用我们来看看IL
当你有这些问题时,为了更好地了解实际发生了什么,最好查看 IL(中间语言),这是你的代码被编译成然后 运行 .NET 运行时间。您可以使用 ildasm 检查已编译的 .NET DLL 和 EXE 的 IL。
连接多个字符串
所以在这里你可以看到在幕后,每个连接的字符串都被调用了 String.Format。
使用一个长字符串
在这里你看到字符串格式只被调用一次,这意味着如果你以这种方式谈论性能会稍微好一些。