SA1118: ParameterMustNotSpanMultipleLines

StyleCop

TypeName

ParameterMustNotSpanMultipleLines

CheckId

SA1118

Category

Readability Rules

Cause

A parameter to a C# method or indexer, other than the first parameter, spans across multiple lines.

Rule Description

To prevent method calls from becoming excessively complicated and unreadable, only the first parameter to a method or indexer call is allowed to span across multiple lines. The exception is an anonymous method passed as a parameter, which is always allowed to span multiple lines. A violation of this rule occurs whenever a parameter other than the first parameter spans across multiple lines, and the parameter does not contain an anonymous method.

For example, the following code would violate this rule, since the second parameter spans across multiple lines:

    return JoinStrings(

        "John",

        "Smith" +

        " Doe");

 

When parameters other than the first parameter span across multiple lines, it can be difficult to tell how many parameters are passed to the method. In general, the code becomes difficult to read.

To fix the example above, ensure that the parameters after the first parameter do not span across multiple lines. If this will cause a parameter to be excessively long, store the value of the parameter within a temporary variable. For example:

    string last = "Smith" +

        " Doe";

 

    return JoinStrings(

        "John",

        last);

 

In some cases, this will allow the method to be written even more concisely, such as:

 

    return JoinStrings("John", last);

 

How to Fix Violations

To fix a violation of this rule, ensure that the parameters after the first parameter do not span multiple lines, unless the parameter contains an anonymous method.