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

前端 未结 9 1419
一整个雨季
一整个雨季 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:36

    It marks the string as a verbatim string literal - anything in the string that would normally be interpreted as an escape sequence is ignored.

    So "C:\\Users\\Rich" is the same as @"C:\Users\Rich"

    There is one exception: an escape sequence is needed for the double quote. To escape a double quote, you need to put two double quotes in a row. For instance, @"""" evaluates to ".

    0 讨论(0)
  • 2020-11-22 01:44

    Putting a @ in front of a string enables you to use special characters such as a backslash or double-quotes without having to use special codes or escape characters.

    So you can write:

    string path = @"C:\My path\";
    

    instead of:

    string path = "C:\\My path\\";
    
    0 讨论(0)
  • 2020-11-22 01:45

    An '@' has another meaning as well: putting it in front of a variable declaration allows you to use reserved keywords as variable names.

    For example:

    string @class = "something";
    int @object = 1;
    

    I've only found one or two legitimate uses for this. Mainly in ASP.NET MVC when you want to do something like this:

    <%= Html.ActionLink("Text", "Action", "Controller", null, new { @class = "some_css_class" })%>
    

    Which would produce an HTML link like:

    <a href="/Controller/Action" class="some_css_class">Text</a>
    

    Otherwise you would have to use 'Class', which isn't a reserved keyword but the uppercase 'C' does not follow HTML standards and just doesn't look right.

    0 讨论(0)
  • 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.)

    0 讨论(0)
  • 2020-11-22 01:52

    It's a verbatim string literal. It means that escaping isn't applied. For instance:

    string verbatim = @"foo\bar";
    string regular = "foo\\bar";
    

    Here verbatim and regular have the same contents.

    It also allows multi-line contents - which can be very handy for SQL:

    string select = @"
    SELECT Foo
    FROM Bar
    WHERE Name='Baz'";
    

    The one bit of escaping which is necessary for verbatim string literals is to get a double quote (") which you do by doubling it:

    string verbatim = @"He said, ""Would you like some coffee?"" and left.";
    string regular = "He said, \"Would you like some coffee?\" and left.";
    
    0 讨论(0)
  • 2020-11-22 01:52

    This is a verbatim string, and changes the escaping rules - the only character that is now escaped is ", escaped to "". This is especially useful for file paths and regex:

    var path = @"c:\some\location";
    var tsql = @"SELECT *
                FROM FOO
                WHERE Bar = 1";
    var escaped = @"a "" b";
    

    etc

    0 讨论(0)
提交回复
热议问题