Special Characters are Truly Special

medieval helmet, armor, and sword
Do you remember the days before string interpolation in C#? I do. It was medieval. Those were truly the dark days of development. We would all use string formatting and concatenation and carried swords around to defend ourselves against giant beasts.


Ok, maybe I'm the only one that was wearing a helmet but I'm kind of a weird guy. I think we can all agree that string concatenation is not all that fun. It might not be as dangerous as dragons, but it can still light your hair on fire when there is a bug in your code just to display some text.


Classes in C# like StringBuilder helped make things safer but it wasn't all that readable. String.Format is easy but keeping the parameters in the right order isn't exactly a no brainer either. What if that code was just in a class behind a view? Can't we just put it in the view? In the code behind it was easily testable, but the context was lost, and it wasn't as simple as what we can do today.

output = String.Format("You are now {0} years old.", years);

I would much rather put my strings together and format them alongside the rest of my markup where its easily read. Often, the better code is the code that gets the job done and is easiest to find and maintain. That's why I love special characters in C#.

There are two special characters in C#. I'll go over "$" first but "@" is just as special.  You have your favorite, I do, but today let's just say that I love both our babies equally.

$ - string interpolation

String interpolation comes in very handing for web developers ASP.NET views but also cross platform developers using Blazor. You can use it in any C# you write but I find it most handy in my views.

Mixing HTML, JavaScript, and CSS and C# can get really unreadable sometimes. If you have to format all your code into variables in a model or controller class before you get to the view code, things can get lost in translation.  Being able to just inline everything together makes a developer's life a lot more fun.
output = $"You are now {years} years old.";
Or better yet, we could just put the whole thing inside our markup.  It works in ASP.NET and Blazor.

<h5>@($"{yourName}, you are {years} years old!")</h5>
I think you see the possibilities and how this is very readable. When you look at the large files and markup around all our variables, we definitely don't want to overthink it. We all want more time back for over thinking how we display text to users.

@ - verbatim identifier

If you're new to .NET and C#, you might not get the need for the verbatim identifier.  The need was created right after string interpolation was created.  As soon as you start parsing a value, you need to have some sort of mechanism to NOT parse a value.  That's where the @ comes in.  
string filename1 = @"c:\documents\files\u0066.txt";
string filename2 = "c:\\documents\\files\\u0066.txt";
In summary, these two special characters might not be as useful as old Excalibur, but chances are they will also not chop your toes off.  If you already use them in your day to day, then I might be preaching to the choir. If you don't use them, try taking them out for a spin.

Documentation Reference: https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/tokens

You can find more great .NET and C# content at the C# Advent 2022 (csadvent.christmas) site.


C# Advent

Comments

Popular posts from this blog

JavaScript should be your favorite language

Why I won't be registering as a blogger in the state of Florida.

Cool new stream about F# from LaylaCodesIt