Wrapping Lines in C#

Problem
You want to know when to wrap long lines of C# code.

Solution
Use the following guidelines to help you decide where to insert a new line to split a long statement.

• Break a long line after a comma but before an operator. Beginning a new line with an operator makes it clear that it’s the result of statement splitting.
• Avoid wrapping a line in the middle of an expression, especially if in the middle of a parenthesized expression.
• Try to keep all items at a given nesting level on the same line.
• Don’t create excessively long lines if you later need to split them. Consider using temp variables to reduce expression complexity.
• Indent all wrapped lines by one tab.

Examples

// This is incorrect. A parenthesized expression is split, leaving an operator on the first line.
result = DoWork(first, second * (third +
    fourth));

// Both of these are correct, but the second one is better.
result = DoWork(first, second
    * (third + fourth));
result = DoWork(first,
    second * (third + fourth));

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s