What is a C # string modifier

This question sounds something like this:

What is @ before a string in C #?

But I already know about the value of @ -character before a string literal in C #.

However, now I saw 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 extra $ coming from @. What does it mean?

+4
source share
2 answers

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

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

Usage example:

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>";
+5
source

A 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

, - $ .

+1

Source: https://habr.com/ru/post/1621574/


All Articles