Declaring a looooong single line string in C#

Cory picture Cory · Oct 14, 2009 · Viewed 45.2k times · Source

Is there a decent way to declare a long single line string in C#, such that it isn't impossible to declare and/or view the string in an editor?

The options I'm aware of are:

1: Let it run. This is bad because because your string trails way off to the right of the screen, making a developer reading the message have to annoying scroll and read.

string s = "this is my really long string.  this is my really long string.  this is my really long string.  this is my really long string.  this is my really long string.  this is my really long string.  this is my really long string.  this is my really long string.  ";

2: @+newlines. This looks nice in code, but introduces newlines to the string. Furthermore, if you want it to look nice in code, not only do you get newlines, but you also get awkward spaces at the beginning of each line of the string.

string s = @"this is my really long string.  this is my long string.
             this line will be indented way too much in the UI. 
This line looks silly in code.  All of them suffer from newlines in the UI.";

3: "" + ... This works fine, but is super frustrating to type. If I need to add half a line's worth of text somewhere I have to update all kinds of +'s and move text all around.

string s = "this is my really long string.  this is my long string. " + 
           "this will actually show up properly in the UI and looks " +
           "pretty good in the editor, but is just a pain to type out " +
           "and maintain";

4: string.format or string.concat. Basically the same as above, but without the plus signs. Has the same benefits and downsides.

Is there really no way to do this well?

Answer

user151323 picture user151323 · Oct 14, 2009

There is a way. Put your very long string in resources. You can even put there long pieces of text because it's where the texts should be. Having them directly in code is a real bad practice.