C
C#•3y ago
androidui

raw string vs verbatin string [Answered]

what is the difference between @" and """
16 Replies
TimberStalker
TimberStalker•3y ago
""" ignores the whitespace that comes before the start of the string on each line so that you can make string literals more readable. It also make it easier to but " characters in the string, as you need a sequence of three to close it. with a verbatim string you would do something like this
var json = @"
{
""Id"" : ""2874234""
""User"" : ""Joe""
}";
var json = @"
{
""Id"" : ""2874234""
""User"" : ""Joe""
}";
333fred
333fred•3y ago
(That isn't a correct example)
TimberStalker
TimberStalker•3y ago
with a raw string you could do this
var json = """
{
"Id" : "2874234"
"User" : "Joe"
}
""";
var json = """
{
"Id" : "2874234"
"User" : "Joe"
}
""";
333fred
333fred•3y ago
You need to escape the "s
TimberStalker
TimberStalker•3y ago
oops
333fred
333fred•3y ago
Still wrong. "" is how you escape quotes in verbatim strings The idea with raw strings is that, no matter text input you have, you will be able to copy/paste it directly into the string content
TimberStalker
TimberStalker•3y ago
i dont write enough raw literals apparently
333fred
333fred•3y ago
var json = """
{
"Id" : "2874234"
"User" : "Joe"
}
""";
var json = """
{
"Id" : "2874234"
"User" : "Joe"
}
""";
Is how you write that one
TimberStalker
TimberStalker•3y ago
ya, never used verbatim strings before
333fred
333fred•3y ago
I think you might be confusing which is which @" is verbatim """ is raw
TimberStalker
TimberStalker•3y ago
is it?
333fred
333fred•3y ago
Yes
TimberStalker
TimberStalker•3y ago
dammit
333fred
333fred•3y ago
Anyway, to answer the question @AndroidUI: raw strings are a new feature we made to address some of the shortcomings and annoyances with verbatim strings. I'll still probably use verbatim strings for single-line content, but mutli-line I'm pretty much always going to be using raw from now on Remember, though, it's not quite out yet: November is C# 11 release
androidui
androiduiOP•3y ago
🙂
Accord
Accord•3y ago
✅ This post has been marked as answered!
Want results from more Discord servers?
Add your server