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 situa...

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: 06 Mar, 2007 02:10 PM History

Comments

ion
ion
17 Apr, 2008 12:22 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 :)

Brendan
Brendan
18 Apr, 2008 02:15 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]

Brendan
Brendan
18 Apr, 2008 02:19 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]

Chapa
Chapa
21 May, 2008 06:02 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

Maxim
Maxim
02 Nov, 2007 09:05 AM

Sucks, not readable

12 Jul, 2008 04:48 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);

BigBunny101
BigBunny101
13 Jun, 2009 08:41 PM

Does anyone know what is the equivalent of @ in C or C++?

timm
timm
30 May, 2009 04:55 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.

EvilNoodle
EvilNoodle
12 Jan, 2009 08:42 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!

19 Feb, 2009 01:03 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

Chris
Chris
01 Jul, 2009 01:58 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.

ivan kirachen
ivan kirachen
29 Jan, 2010 08:15 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.