Do you use Environment.NewLine to make a new line in your string?

Updated by Brook Jeynes [SSW] 2 years ago. See history

123
<introEmbed body={<> When you need to create a new line in your string, make sure you use Environment.NewLine, and then literally begin typing your code on a new line for readability purposes. </>} />
string strExample = "This is a very long string that is \r\n not properly implementing a new line.";

❌ Figure: Bad example - The string has implemented a manual carriage return line feed pair ` `

string strExample = "This is a very long string that is " + Environment.NewLine +
" properly implementing a new line.";

✅ Figure: OK example - The new line is created with Enviroment.NewLine (but strings are immutable)

var example = new StringBuilder();
example.AppendLine("This is a very long string that is ");
example.Append(" properly implementing a new line.");

✅ Figure: Good example - The new line is created by the StringBuilder and has better memory utilisation

Acknowledgements

Adam Cogan
Matt Wicks
Related rules

Need help?

SSW Consulting has over 30 years of experience developing awesome software solutions.

We open source.Loving SSW Rules? Star us on GitHub. Star