声明,在C#6使用字符串插值长串 [英] Declaring long strings that use string interpolation in C# 6

查看:168
本文介绍了声明,在C#6使用字符串插值长串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通常通过将它们包裹的长字符串:

I usually wrap long strings by concatenating them:

Log.Debug("I am a long string. So long that I must " +
    "be on multiple lines to be feasible.");

这是完全有效的,因为编译器处理字符串的串联。我还认为,为了解决这个问题(最干净的方式的选项。在这里称重)

This is perfectly efficient, since the compiler handles concatenation of string literals. I also consider it the cleanest way to handle this problem (the options are weighed here).

这与方法行之有效的String.Format

Log.Debug(String.Format("Must resize {0} x {1} image " +
    "to {2} x {3} for reasons.", image.Width, image.Height,
    resizedImage.Width, resizedImage.Height));



不过,现在我希望永远不会使用的String.Format 再次在这些情况下,因为C#6的字符串插值更可读。 我担心的是,我不再有一个有效的,但干净的方式来格式化长字符串。

However, I now wish to never use String.Format again in these situations, since C# 6's string interpolation is much more readable. My concern is that I no longer have an efficient, yet clean way to format long strings.

我的问题是,如果编译器能以某种方式优化像

My question is if the compiler can somehow optimize something like

Log.Debug($"Must resize {image.Width} x {image.Height} image " +
    $"to {resizedImage.Width} x {resizedImage.Height} for reasons.");



入上述的String.Format 或等值如果有,我可以使用另一种方法将不效率(由于不必要的串联),同时也保持我的代码结构清晰(按照上面的链接中提出的观点)。

into the above String.Format equivalent or if there's an alternative approach that I can use that won't be less efficient (due to the unnecessary concatenation) while also keeping my code cleanly structured (as per the points raised in the link above).

推荐答案

目前,诸如此类的优化似乎是不可能的。

Currently, that sort of optimization seems impossible.

这程序:

var name = "Bobby Tables";
var age = 8;
String msg = $"I'm {name} and" +
    $" I'm {age} years old";



被编译为如果你这样写:

is compiled as if you had written:

var name = "Bobby Tables";
var age = 8;
String msg = String.Concat(String.Format("I'm {0} and", name),
    String.Format(" I'm {0} years old", age));

您看到摆脱的毗连的难度 - 编译器重写我们的插值文字使用索引格式化了的String.Format 预计,但每串有从0数,其参数天真它们连接起来会导致他们既可以插入名称。为了得到这个正常工作了,就必须要在 $ 解析器调用之间保持状态,这样,第二个字符串被格式化为我M {1}岁的。另外,编译器可以尝试采用同样类型的分析它为字符串的串联。我认为这将是即使串插有副作用法律的优化,但我不会感到惊讶,如果事实证明有下插字符串连接改程序的行为角落情况。无论是听起来不可能的,尤其是考虑到逻辑已经在那里来检测字符串类似的情况,但我可以看到为什么这个功能并没有使它成为首次发布。

You see the difficulty in getting rid of the Concat - the compiler has re-written our interpolation literals to use the indexed formatters that String.Format expects, but each string has to number its parameters from 0. Naively concatenating them would cause them both to insert name. To get this to work out correctly, there would have to be state maintained between invocations of the $ parser so that the second string is reformatted as " I'm {1} years old". Alternatively, the compiler could try to apply the same kind of analysis it does for concatenation of string literals. I think this would be a legal optimization even though string interpolation can have side effects, but I wouldn't be surprised if it turned out there was a corner case under which interpolated string concatenation changed program behavior. Neither sounds impossible, especially given the logic is already there to detect a similar condition for string literals, but I can see why this feature didn't make it into the first release.

我会写在你的感觉是干净和最可读的方式代码,而不必担心微效率低下,除非它们被证明是一个问题。有关代码的主要是为人类所了解的老话在这里举行。

I would write the code in the way that you feel is cleanest and most readable, and not worry about micro-inefficiencies unless they prove to be a problem. The old saying about code being primarily for humans to understand holds here.

这篇关于声明,在C#6使用字符串插值长串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆