Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All
Expand Minimize
Important This document may not represent best practices for current development, links to downloads and other resources may no longer be valid. Current recommended version can be found here.

Compiler Warning (level 3) CS1718 

Error Message

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 T-SQL, the programming language used by SQL Server. Developers familiar with T-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 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++;
        }
    }
}

Community Additions

ADD
Show:
© 2015 Microsoft