Compiler Warning (level 3) CS1718

Comparison made to same variable; did you mean to compare something else?

If you meant to compare to something else, then correct the statement.

But another possibility is that you were testing for true or false, and were doing so by statements such as if (a == a) (true) or if (a < a) (false). It is better to use if (true) or if (false), for the following two reasons:

  • It is simpler: it is always clearer to simply say what you mean.

  • It helps avoid confusion: a new feature of C# 2.0 is nullable value types, which are analogous to the value null in T-SQL, the programming language used by SQL Server. Developers familiar with T-SQL might be concerned about the effect of nullable value types on expressions such as if (a == a), because of the use of ternary logic in T-SQL. If you use true or false, you avoid this possible confusion.

Example

The following code generates warning CS1718.

// CS1718.cs  
using System;  
public class Tester
{  
    public static void Main()
    {
        int i = 0;  
        if (i == i) { // CS1718.cs  
        //if (true) {
            i++;  
        }  
    }  
}