C
C#•2y ago
androidui

raw string vs verbatin string [Answered]

what is the difference between @" and """
16 Replies
TimberStalker
TimberStalker•2y 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•2y ago
(That isn't a correct example)
TimberStalker
TimberStalker•2y ago
with a raw string you could do this
var json = """
{
"Id" : "2874234"
"User" : "Joe"
}
""";
var json = """
{
"Id" : "2874234"
"User" : "Joe"
}
""";
333fred
333fred•2y ago
You need to escape the "s
TimberStalker
TimberStalker•2y ago
oops
333fred
333fred•2y 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•2y ago
i dont write enough raw literals apparently
333fred
333fred•2y ago
var json = """
{
"Id" : "2874234"
"User" : "Joe"
}
""";
var json = """
{
"Id" : "2874234"
"User" : "Joe"
}
""";
Is how you write that one
TimberStalker
TimberStalker•2y ago
ya, never used verbatim strings before
333fred
333fred•2y ago
I think you might be confusing which is which @" is verbatim """ is raw
TimberStalker
TimberStalker•2y ago
is it?
333fred
333fred•2y ago
Yes
TimberStalker
TimberStalker•2y ago
dammit
333fred
333fred•2y 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
androidui•2y ago
🙂
Accord
Accord•2y ago
✅ This post has been marked as answered!