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

前端 未结 9 1422
一整个雨季
一整个雨季 2020-11-22 01:04

This is a .NET question for C# (or possibly VB.net), but I am trying to figure out what\'s the difference between the following declarations:

string hello =          


        
9条回答
  •  难免孤独
    2020-11-22 01:46

    Since you explicitly asked for VB as well, let me just add that this verbatim string syntax doesn't exist in VB, only in C#. Rather, all strings are verbatim in VB (except for the fact that they cannot contain line breaks, unlike C# verbatim strings):

    Dim path = "C:\My\Path"
    Dim message = "She said, ""Hello, beautiful world."""
    

    Escape sequences don't exist in VB (except for the doubling of the quote character, like in C# verbatim strings) which makes a few things more complicated. For example, to write the following code in VB you need to use concatenation (or any of the other ways to construct a string)

    string x = "Foo\nbar";
    

    In VB this would be written as follows:

    Dim x = "Foo" & Environment.NewLine & "bar"
    

    (& is the VB string concatenation operator. + could equally be used.)

提交回复
热议问题