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

前端 未结 9 1423
一整个雨季
一整个雨季 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: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.";
    

提交回复
热议问题