4

This question sounds similar to this:

What's the @ in front of a string in C#?

But I'm already aware of the meaning of the @-character in front of a string literal in C#.

However now I've seen this in an example:

var xml = $@"<toast>
    <visual>
        <binding template='ToastGeneric'>
            <text>text</text>
        </binding>
    </visual>

    <audio src='ms-winsoundevent:Notification.Looping.Alarm10' loop='true'/>
</toast>";

There is an additional $ coming with the @. What does this mean?

Community
  • 1
  • 1
stefan.s
  • 3,489
  • 2
  • 30
  • 44
  • Although it does use the string interpolation modifier, it doesn't actually use string interpolation at all. – Yuval Itzchakov Dec 24 '15 at 07:01
  • 1
    @YuvalItzchakov Yes, but I guess that was my mistake since I dind't realize how it works. I tried a string.format afterwards and it failed. Before posting the question I removed my {0} from the text tag, because I thought that would be another problem. – stefan.s Dec 24 '15 at 07:14

2 Answers2

5

it's interpolated string, a new feature for C# 6.0 (https://msdn.microsoft.com/en-us/library/dn961160.aspx)

Basically, it replace string.Format("", params); in old C# version

Example usage:

var str = "test";
var xml = $@"<toast>
    <visual>
        <binding template='ToastGeneric'>
            <text>{str}</text>
        </binding>
    </visual>
    <audio src='ms-winsoundevent:Notification.Looping.Alarm10' loop='true'/>
</toast>";
Kien Chu
  • 4,735
  • 1
  • 17
  • 31
  • 1
    Okay, I see. My question should then really be about $ not @$, since they are not connected. Maybe that's the reason, I haven't found the information... – stefan.s Dec 24 '15 at 06:49
1

The $ sign denotes an interpolated string in C#.

MSDN: https://msdn.microsoft.com/en-us/library/dn961160.aspx

Usage example:

string zzz = "world";
string helloWorld = $"hello {zzz}"; // hello world

There are no curly braces inside the string in your code, so no actual value injection occurs - you can remove the $ and still get identical results.

Kirill Shlenskiy
  • 9,367
  • 27
  • 39