This documentation is archived and is not being maintained.

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 you should simply 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 simply say if (true) or if (false). There are two reasons for this:

  • 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 Transact-SQL, the programming language used by SQL Server. Developers familiar with Transact-SQL might be concerned about the effect of nullable types on expressions such as if (a == a), because of the use of ternary logic in Transact-SQL. If you use true or false, you avoid this possible confusion.

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++;
        }
    }
}
Show: