Requesting a new escape sequence that is equal to Environment.NewLine #3241
Replies: 6 comments
-
An interesting idea. One difficulty with this is that currently plain string literals are embedded in IL which means that they take up no extra memory at runtime. Instead you just get a pointer to the string in metadata. |
Beta Was this translation helpful? Give feedback.
-
@YairHalberstadt |
Beta Was this translation helpful? Give feedback.
-
Given a string, in a given scope, and an escape sequence... the compiler would need to discover what should be interpolated there. I'll also guess that a conflict should be a compiler error. My first idea was that you could add an attribute, so that the developer could define the escape sequence with an attribute parameter, and it would be resolved with interpolating the member on which the attribute was applied, considering scope rules... However the meaning of literal strings being changed by another file this way bother me. Perhaps it should be scoped by files instead. A new using directive... how about:
I did consider:
However, there it is not clear that "z" is an escape sequence. I also did consider:
However, that could give the impression that you could put there any string, not only those that begin with In my suggestion
I did also consider something like I'm just tossing ideas, don't mind me. |
Beta Was this translation helpful? Give feedback.
-
Escape sequences are expanded at compile time and the characters inserted into the literal string. That would be impossible here because the value of I'd suggest that it's probably easier to map |
Beta Was this translation helpful? Give feedback.
-
Another possibility is a new property on Although I'm sure there are implementation pains there as well. |
Beta Was this translation helpful? Give feedback.
-
In the past I always used the following:
internal static class CommonUtils
{
public static readonly string NL = System.Environment.NewLine;
} And in any other file: using System;
using static CommonUtils;
namespace App
{
public class Program
{
public static void Main(string[] argv)
{
Console.WriteLine($"This is a{NL}multi-lined{NL}string!");
}
}
} I think that this proposal has some potential, however, I currently see the same problem as @YairHalberstadt does. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
For cross-platform code, writing
$"I love C#.{Environment.NewLine}How about you?"
reduces the readability. It will be better if we define a new escape sequence, for example\z
, that is equal toEnvironment.NewLine
. So the above literal string becomes more readable,"I love C#.\zHow about you?"
.I hope my request does make sense. Thank you in advance.
Beta Was this translation helpful? Give feedback.
All reactions