Declaring a looooong single line string in C#

后端 未结 11 990
礼貌的吻别
礼貌的吻别 2021-02-05 00:58

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 o

相关标签:
11条回答
  • 2021-02-05 01:51

    You could use StringBuilder

    0 讨论(0)
  • 2021-02-05 01:52

    I either just let it run, or use string.format and write the string in one line (the let it run method) but put each of the arguments in new line, which makes it either easier to read, or at least give the reader some idea what he can expect in the long string without reading it in detail.

    0 讨论(0)
  • 2021-02-05 01:54

    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.

    0 讨论(0)
  • 2021-02-05 01:58

    Does it have to be defined in the source file? Otherwise, define it in a resource or config file.

    0 讨论(0)
  • 2021-02-05 01:59

    For really long strings, I'd store it in XML (or a resource). For occasions where it makes sense to have it in the code, I use the multiline string concatenation with the + operator. The only place I can think of where I do this, though, is in my unit tests for code that reads and parses XML where I'm actually trying to avoid using an XML file for testing. Since it's a unit test I almost always want to have the string right there to refer to as well. In those cases I might segregate them all into a #region directive so I can show/hide it as needed.

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