Strings in C# 11 just got a whole lot better

preview_player
Показать описание

Hello everybody I'm Nick and in this video I will introduce you to a new C# 11 feature called raw string literals. Raw string liretals allows us to deal with string in their raw form without having to format them in a specific way in our code and escape them. It makes working wiht JSON an absolute breeze and I can't wait to use them when C# 11 comes out.

Don't forget to comment, like and subscribe :)

Social Media:

#csharp #dotnet #csharp11
Рекомендации по теме
Комментарии
Автор

Nick: "And just so the errors are not distracting in Rider i'm gonna go to VSCode"
VSCode : Are you sure about that

ledrake
Автор

I've seen so many languages try to come up with raw string literals and end up with something that's ugly or annoying to use in some ways, but this is my favourite. The variable number of " and $ is a really flexible and elegant solution.

chylex
Автор

This is actually really similar to the way Python does this thing, and honestly, C# here have done it so much better with ignoring extra spaces and stuff, while Python just has raw string that you can't align with the rest of the code

capsey_
Автор

The triple quotation marks is also used for multiline string in Swift. I'm really glad they adopted this feature and it's nice how flexible it is to handle sequential characters and string interpolation.

nitesh-maharaj
Автор

Yes, please, keep showing the edge cases, that's the best part!

paulostradioti
Автор

This is cool!
I have projects with LOTS of complex SQL queries (some for optimization reasons, some from legacy code, etc...) that are simply ugly to see in code, or difficult to follow due to formatting.
If I try for make them readable, I will then have difficulties following from profiling tools.
This feature will definetively make both my code readable and debuggable, will use for sure!

claudiovalerio
Автор

When creating a source generator, I so badly wanted something like this! Creating nicely formatted code is such a pain in those things...

badger-
Автор

How about opening """ on new line. So you do not need to have any extra on each line of variable declare and assignment sign.

EpsilneR
Автор

Works pretty well and is not just for net7.0. It can be used with Visual studio 2022 in any project (even Framework) if you put the in the project file. Pretty cool.

antonknieriemen
Автор

So, it's 11PM right now and I clicked this thinking it was going to be about string music in the key of C# for some reason, and I mean it wasn't that but dang this is still cool. Awesome video!

joshbishop
Автор

Wow! First thing I thought it's gonna be a gimmick 'cause you can't align it in a code but anchoring alignment to a position of triple quotes is brilliant. At last I'm satisfied with a new C# feature 😊

Max_Jacoby
Автор

I like this new feature. I will be using it for sure in my code. So many double quotes and dolar signs may look wierd but man you won't be using more than 3 quotes in 99% of cases

antonmartyniuk
Автор

One of the few times when VB got this feature earlier (like 10 years ago) in the form of XML literals.

cuachristine
Автор

I've just started learned c#. Love how clear and informative this video is.

Nuan
Автор

I use JSON in some unit tests. I just open it from a file. Then I get JSON syntax highlighting in that file.

jesslilly
Автор

I've always felt that this should be some sort of editor feature, the editor through context or some form of metadata tag (comment) knows that this is a multi-line / raw string, and there's some known encoding style for it, and then a smaller "windowed" area in the editor is created for this variable.

If this sort of thing (other than the interpolation) was an editor tool, then we get it for all languages.

prozacgodretro
Автор

Nick: Look at this cool new C# feature.
Me, a unity dev: ...

KeyboardKrieger
Автор

Immediately installed dotnet 7 so I could use this in a game I'm making right now! Thanks for this video!

merthsoft
Автор

Thank you very much for your introduction to C# raw string literals.

Two questions:
1. I saw earlier that a slightly different format for this was discussed, which was inspired by C++.

E.g.
R"(<a href="file">C:\Program Files\</a>)"

I'm interested in your opinion: is the current format really better and more convenient than the one discussed earlier by folks in csharplang repo issue "Proposal: Support "raw" string literals"?

2. For similar tasks, I usually use this alignment, a little simpler. What do you think? Will it work with current format for C# raw string literals?

E.g.
var text =
"""
{
"Name": "Anton",
"Age": 28
}
""";

anreton
Автор

I like this and I'll definitely use it. I dynamically build HTML for creating reports, so this is a massive addition for that. But I could see that the 7 quote scenarios could cause a mess with you're dynamically building strings. But, obviously, it will work fine for 99% of use cases.

tchpowdog