The C# @ String Literal

C# is a pretty sweet language, and there are many, many, many little things that just make the code that much nicer. Have you ever been in a situation where for some reason you NEEDED to have a string/code fragment/js fragment/sql statement inline in your code because you don't believe in resource files or stored procedures?

Aside from whatever great debate about "if you should or shouldn't". If you are going to, please, please, learn to take advantage of what C# has to offer, yes I'm talking about the '@' string literal.

Now I'm not going to target anyone in particular, but my feeling is that VB as a language seems to be more notorious for having hundreds of lines of String.Append()s. String.Append is the absolute worst, hardest and most illegible way of possibly including your fragment inline in code.

There are also the programmers that 'know of' the @ symbol and prefix all their strings with it because it looks cool or something. Doing this without reason may actually alter the behaviour you excepted because it causes escape sequences to NOT be processed.
Eg. "c:\\my\\file.txt" could be done as @"c:\my\file.txt"
This means your \n and \t or whatever it is will also not be processed.

I'd say the most precious and special super power of a string literal is its multi-line ability. So instead of writing code that may look something like:
var1 += @"some text" + Environment.NewLine;
var1 += @"some more text" + Environment.NewLine;
var1 += @"even more text" + Environment.NewLine;

You could just use the @-quoting and write:
var1 = @"some text
some more text
event more text";

And yes that will compile perfectly fine. Best of all, even without going into the debate of "having inline fragments" at least I can read it and change it.

General .NET Code Snippets
Posted by: Brendan Kowitz
Last revised: 21 Sep 2013 12:15PM

Comments

11/2/2007 9:05:19 AM
Sucks, not readable
ion
ion
4/17/2008 12:22:53 PM
have you ever heard of such thing sometimes named as "performance"? if you dont know how @ or escaping and processing works - it sometimes might look like code looks cooler with some "strange chars". Not if you know what they do and why they are in the code :)

no offence. good luck :)
4/18/2008 2:15:17 AM
I think your missing the point of the post.

There examples of when and when not to use the 'strange chars'.

This post also seems to be thinking along the same lines:
"Using string literals directly in your code is a better approach than using string constants! It can make the code easier to understand and has no adverse impact on performance"
[http://dotnet.sys-con.com/read/46342.htm]
4/18/2008 2:19:59 AM
I think you're missing the point of the post.

There's examples of when and when not to use the 'strange chars'.

This post also seems to be thinking along the same lines:
"Using string literals directly in your code is a better approach than using string constants! It can make the code easier to understand and has no adverse impact on performance"
[http://dotnet.sys-con.com/read/46342.htm]
5/21/2008 6:02:08 AM
nice
the @ sign is very helpful, but...
it does not allow using any string functions.
is there any way of manipulating the system so it will allow the usage of the 'replace' string functuion.

{~:
Chapa
7/12/2008 4:48:22 PM
Lets say str is the string,
we can do replace on it like this:
str = str.Replace(Environment.NewLine, " ");
You might wanna encode special characters as well for good like this:
str = HttpUtility.HtmlEncode(str);
1/12/2009 8:42:03 AM
Thanks! I found this helpful!
I read code with this @ sign in front of the strings and never quite knew what it was for!

Thanks again!
2/19/2009 1:03:02 PM
Using the @ is an awsome part of C#
but using var += is not good
use system.text.stringbuilder, which is immutable. += not immutable so bad...
stringbuilder is availiable in vb and c#
I'm writing this for the future googler's
Dkultra
5/30/2009 4:55:08 AM
For the IE Browser I use the hell out of Fidler http://wiwapia.com/en/fidler to look at all traffic going across the wire. For FireFox, you can use the FireBug plugin http://wiwapia.com/en/FireBug . There is a "Net" tab that will show you request information that is going across the wire. Most of the time you can use one of these tools to see what URL was requested in order to start a download. You can also view all the get and post information that might need to be sent in order to have your request succeed.
6/13/2009 8:41:11 PM
Does anyone know what is the equivalent of @ in C or C++?
7/1/2009 1:58:43 PM
I don't think the @ operator has a C or C++ equivalent, although I could be mistaken. However, I've never seen anything similar and I've been using C++ for a while.
1/29/2010 8:15:38 AM
Well in C/C++ sting literal like "just string" is just a char* (char pointer) not an object.
I think this is the reason for no '@' equivalent.

No new comments are allowed on this post.