Read the syntax
Comparison operators produce bool values. && requires both operands to be true, || requires at least one, and ! negates a bool; && and || skip their right side when the left side determines the answer.
For variables typed string, == compares text content. ReferenceEquals checks object identity instead; do not infer general object equality rules from interned string literals.
Syntax
== != < > <= >= && || !Examples
Comparison Operators
Comparing values to produce a boolean result.
int a = 5, b = 3;
Console.WriteLine(a == b); // false
Console.WriteLine(a != b); // true
Console.WriteLine(a > b); // true
Console.WriteLine(a <= b); // false- Comparing values to produce a boolean result.
- Comparison operators produce bool values. && requires both operands to be true, || requires at least one, and ! negates a bool; && and || skip their right side when the left side determines the answer.
- With a equal to 5 and b equal to 3, the four comparisons print False, True, True and False. Each expression is evaluated independently.
Logical Operators
Combining boolean expressions with &&, ||, and !.
int age = 25;
bool hasId = true;
if (age >= 18 && hasId)
{
Console.WriteLine("Entry allowed");
}
bool isWeekend = false;
bool isHoliday = true;
if (isWeekend || isHoliday)
{
Console.WriteLine("No work today");
}
bool isBanned = false;
if (!isBanned)
{
Console.WriteLine("Access granted");
}- Combining boolean expressions with &&, ||, and !.
- Comparison operators produce bool values. && requires both operands to be true, || requires at least one, and ! negates a bool; && and || skip their right side when the left side determines the answer.
- All three conditions are true with these values, so the program prints Entry allowed, No work today and Access granted. Change hasId or the two day flags to observe && versus ||.
String Equality with ==
Unlike Java, C#'s == operator compares string content, not just references, because string overloads it.
string a = "hello";
string b = new string(new[] { 'h', 'e', 'l', 'l', 'o' });
Console.WriteLine(a == b); // True: same characters
Console.WriteLine(a.Equals(b)); // True: same content
Console.WriteLine(ReferenceEquals(a, b)); // False: different objects- Unlike Java, C#'s == operator compares string content, not just references, because string overloads it.
- Comparison operators produce bool values. && requires both operands to be true, || requires at least one, and ! negates a bool; && and || skip their right side when the left side determines the answer.
- The two strings have equal characters, so a == b and a.Equals(b) are true. ReferenceEquals is false because the second string was constructed as a separate object.
Best practices
- Take advantage of short-circuit evaluation: put null checks first in && expressions (obj != null && obj.Value > 0) to avoid a NullReferenceException
- Trust == for string comparisons in C# - unlike Java, it compares content because string overrides the equality operator
- Wrap multi-condition logical expressions in parentheses for clarity, even when not strictly required by operator precedence
- Use string.Equals(a, b, StringComparison.OrdinalIgnoreCase) instead of .ToLower() == .ToLower() for case-insensitive comparisons - it avoids unnecessary allocations
Runtime & versions
.NET runtime. Feature availability depends on the language and runtime version used by your project; consult the official reference below.
C# language reference