Multiline string literal in C#

后端 未结 13 1464
梦谈多话
梦谈多话 2020-11-22 11:15

Is there an easy way to create a multiline string literal in C#?

Here\'s what I have now:

string query = \"SELECT foo, bar\"
+ \" FROM table\"
+ \" W         


        
13条回答
  •  名媛妹妹
    2020-11-22 11:22

    The problem with using string literal I find is that it can make your code look a bit "weird" because in order to not get spaces in the string itself, it has to be completely left aligned:

        var someString = @"The
    quick
    brown
    fox...";
    

    Yuck.

    So the solution I like to use, which keeps everything nicely aligned with the rest of your code is:

    var someString = String.Join(
        Environment.NewLine,
        "The",
        "quick",
        "brown",
        "fox...");
    

    And of course, if you just want to logically split up lines of an SQL statement like you are and don't actually need a new line, you can always just substitute Environment.NewLine for " ".

提交回复
热议问题