C
C#3y ago
Simo

❔ multi-line strings

hi, if I have a string like
@"this
is a multi
line string!"
@"this
is a multi
line string!"
when my code is formatted, tabs get inserted into my string so it ends up like:
@"this
is a multi
line string!"
@"this
is a multi
line string!"
What is the best way around this? replacing new lines with \n?
13 Replies
vdvman1
vdvman13y ago
You can use a raw string literal (new feature in C# 11):
"""
this
is a multi
line string!
"""
"""
this
is a multi
line string!
"""
Simo
SimoOP3y ago
is it safe to set my language version to C# 11 preview? (still on .net6)
sibber
sibber3y ago
upgrade to 7
Simo
SimoOP3y ago
i guess i need to for this - just cant be bothered right now cuz i gotta update and re-test my docker deploys its on my plate for later in the week though
sibber
sibber3y ago
well for now you can just not indent
vdvman1
vdvman13y ago
There are features in C# 11 that require .NET 7 (namely ref fields IIRC), but if you are careful about what you use it should still work fine (I'm even using C# 11 in a .NET 5 project)
Simo
SimoOP3y ago
ive currently got "\n" in my string and keeping it on a single line, i think that might work ok
sibber
sibber3y ago
oh right you can actually set the lang version to 11
vdvman1
vdvman13y ago
Yup, it's technically unsupported, but as long as you are careful not to use any features that require runtime changes it works perfectly fine
Simo
SimoOP3y ago
i saw 11 in the dropdown in Rider but assumed latest preview would be the same or better
vdvman1
vdvman13y ago
I don't believe there's anything newer than 11 at the moment Usually it's best to stick with a specific lang version, rather than using preview Preview is the absolute latest, and may have features that get changed later in development
Angius
Angius3y ago
An update from .NET 6 to .NET 7 is really as simple as just changing the version in the csproj Same for the language version
Accord
Accord3y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.

Did you find this page helpful?