Using the literal '@' with a string variable

后端 未结 6 774
Happy的楠姐
Happy的楠姐 2020-12-17 01:42

I have a helper class pulling a string from an XML file. That string is a file path (so it has backslashes in it). I need to use that string as it is... How can I use it lik

6条回答
  •  清酒与你
    2020-12-17 02:16

    The @"" just makes it easier to write string literals.

    string (C# Reference, MSDN)

    Verbatim string literals start with @ and are also enclosed in double quotation marks. For example:

    @"good morning" // a string literal

    The advantage of verbatim strings is that escape sequences are not processed, which makes it easy to write, for example, a fully qualified file name:

    @"c:\Docs\Source\a.txt" // rather than "c:\\Docs\\Source\\a.txt"

    One place where I've used it is in a regex pattern:

    string pattern = @"\b[DdFf][0-9]+\b";

    If you have a string in a variable, you do not need to make a "literal" out of it, since if it is well formed, it already has the correct contents.

提交回复
热议问题