6

So, in C#, are there any tips or guidelines as to how to write "clean" multiline strings? I mean, imo it doesn't look as neat when the code looks like:

static string kindOfLongVariableName = @"First line of string.
Second line of string...";

With the first line sort of hovering on the side. What I'm used to is with Python where you can just:

variable_name = """'\
First line.
Second line.
"""

And use the '\' to escape the newline, but that obviously doesn't work in C#. Can anyone point me in a direction to start looking? The docs are a bit confusing and not very beginner friendly. :/

Comments
  • 1
    Like this:

    var x = @"first line

    second line

    third line

    last line";

    That @ is the key here, no need for \n (new line char for strings)
  • 1
    @gitpush Yeah, no, sorry I should've clarified a bit better. What bothers me is that it makes the code harder to read, and it's more difficult to see what the result is going to be. :/ I was mostly wondering just for the sake of convenience, but if there's no way around it I'll just deal with it.

    Oh, and I think maybe a better way to write the Python example might've been:

    variable_name = \

    """First line.

    Second line."""
  • 0
    @Navigatr but its exactly like the python example except that \ is replaced with @ and the first line must always be after @ symbol

    I'm not sure how not readable it is :\
  • 2
    @gitpush Alright, let's try this. :P Sharing example as attached image.
  • 0
    @Navigatr Not sure why that second/third line not aligned under the first line, it usually should unless you configured your IDE to not do so
  • 1
    @gitpush Doesn't the whitespace get included in the string though?
  • 0
    @Navigatr Yes sorry my mistake :S
  • 1
    Put stuff like this into a resource entry? :D I know its not a direct answer to your problem but how you can prevent it.
  • 0
    @hypervtechnics I'll have to see if there's enough variables for that to feel necessary, but how would I go about that? :o
  • 1
    What @jschmold said and you could also use string.Join
  • 0
    @jschmold @electrineer I hadn't really thought much about that tbh. I'll keep both of those things in mind, thanks. :)
  • 0
    @jschmold Yeah, I know, it's really frustrating. :(
  • 1
    @jschmold use Environment.NewLine instead of \n
  • 0
    @jschmold Look into <Appnamespace>.Properties.Resources
  • 1
    For long strings it's really better to keep them in separate resource files. Not to mention its way easier to make localized version of the app having strings in separate place.
  • 0
    Honestly, I’d go down the resource file route.

    It’ll make it way neater, and you get the benefit of possible localisation.
  • 0
    @Brolls Yeah, but this is literally just a class assignment, nothing big by any means. Still, I've been considering it, problem being that I don't know exactly how it works. Would the strings just be stored in a regular .txt file? I'm confused and can't seem to find anything that explains it well when I search around.
  • 0
    @Navigatr in .NET projects you have a built in resource system.

    You want this:

    https://docs.microsoft.com/en-us/...
  • 1
    @Brolls I won't be able to look at it tonight, but I'll try and read through it this weekend, thank you. :)
  • 1
    @Navigatr https://docs.microsoft.com/en-us/...

    ^ this might be a better starting point
Add Comment