Why do the linebreaks in multiline @-strings use that of the .cs code file instead of Environment.NewLine
or a constant one?
#8237
Replies: 5 comments 2 replies
-
This is the equivalent python code: a="""
"""
print(len(a)) It always outputs |
Beta Was this translation helpful? Give feedback.
-
If you want/need a definite/consistent line ending for other developers, you can enforce it with an editor config. |
Beta Was this translation helpful? Give feedback.
-
Your tools should not be making changes to your source files. Source files contain content. So changing something like line endings literally changes the meaning of code. We don't use Env.NewLine for the same reason. The meaning of your code would change on different computers. If you don't want your code meaning to change, don't have your tools change the content. For something like git/GitHub there are options for this. |
Beta Was this translation helpful? Give feedback.
-
Git and GitHub came out in 2005 and 2008. Many years after c# was already out. No clue why they may have picked an option that changes the content of source files. Fortunately, there's an easy fix for that. Just set the right options in your .gitattributes file so that git doesn't munge anything inappropriately. See https://docs.github.com/en/get-started/getting-started-with-git/configuring-git-to-handle-line-endings for more details. |
Beta Was this translation helpful? Give feedback.
-
What's the alternative? Having the length depend on whether the compiler happened to be running on Windows? Or making these string literals non-constant, rebuilding the string at runtime to insert Environment.NewLines each time the code runs? |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
If I save the above code in CRLF linebreak, it will output
2
. However, if I save it in LF linebreak, it will output1
This makes multiline string TOTALLY USELESS if I want to upload my code onto GitHub, because another user (or GitHub Actions) with a different git config may get wrong result.
Beta Was this translation helpful? Give feedback.
All reactions