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).
This approach worked well with 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));
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.");
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).
String.Format
– SamaraString.Format
explicitly). I'm mostly asking in hopes that there may be an alternative approach, compiler option, etc. – Gershamstring.Format
itself (indeed emitting format strings like"Must resize {0} x {1} image "
, which are definitely treated as compile-time constant!), but now we are concatenating results of calls tostring.Format
, whereas the 2 constant format strings ended up as parameters - where they obviously are not concatenated by the compiler – Kinnie$@"..."
) – Geodynamics