SA1119: StatementMustNotUseUnnecessaryParenthesis

StyleCop

TypeName

StatementMustNotUseUnnecessaryParenthesis

CheckId

SA1119

Category

Maintainability Rules

Cause

A C# statement contains parenthesis which are unnecessary and should be removed.

Rule Description

It is possible in C# to insert parenthesis around virtually any type of expression, statement, or clause, and in many situations use of parenthesis can greatly improve the readability of the code. However, excessive use of parenthesis can have the opposite effect, making it more difficult to read and maintain the code.

A violation of this rule occurs when parenthesis are used in situations where they provide no practical value. Typically, this happens anytime the parenthesis surround an expression which does not strictly require the use of parenthesis, and the parenthesis expression is located at the root of a statement. For example, the following lines of code all contain unnecessary parenthesis which will result in violations of this rule:

    int x = (5 + b);

    string y = (this.Method()).ToString();

    return (x.Value);

 

In each of these statements, the extra parenthesis can be removed without sacrificing the readability of the code:

 

    int x = 5 + b;

    string y = this.Method().ToString();

    return x.Value;

 

How to Fix Violations

To fix a violation of this rule, remove the unnecessary parenthesis.