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 asif (a == a)
, because of the use of ternary logic in T-SQL. If you usetrue
orfalse
, 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++;
}
}
}
Feedback
https://aka.ms/ContentUserFeedback.
Coming soon: Throughout 2024 we will be phasing out GitHub Issues as the feedback mechanism for content and replacing it with a new feedback system. For more information see:Submit and view feedback for