Codectionary / Developer documentation / C#

Comparison & Logical Operators

Comparison operators (==, !=, <, >, <=, >=) compare two values and produce a bool. Logical operators (&&, ||, !) combine or invert boolean expressions, with && and || short-circuiting so the second operand is skipped once the result is already determined. For strings, == compares content by default (unlike some languages), since string overrides the equality operator.

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

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");
}

String Equality with ==

Unlike Java, C#'s == operator compares string content, not just references, because string overloads it.

string a = "hello";
string b = "hel" + "lo";

Console.WriteLine(a == b);         // true - compares content
Console.WriteLine(a.Equals(b));     // true - same result

object x = "hello";
object y = "hello";
Console.WriteLine(x == y);  // true for string literals (interned)

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

At a glance

Purpose
Applications on the .NET platform
File extension
.cs
Runs in
.NET runtime
Usually used with
.NET SDK and libraries

Specifications & further reading

Related C# documentation

Variables & Data Types
C# is a statically-typed language, meaning every variable's type is fixed at compile time. Built-in value types include int, double, decimal, bool, and char, while string and object are reference types. The var keyword lets the compiler infer a variable's type from its initializer, but the variable is still strongly typed underneath - it just saves you from writing the type name explicitly.
Console.WriteLine / ReadLine
The Console class, from the System namespace, provides the standard way to read from and write to the terminal in a C# console application. Console.WriteLine() prints a value followed by a newline, Console.Write() prints without one, and Console.ReadLine() reads a full line of text typed by the user, always returning it as a string.
Comments & XML Documentation
C# supports single-line comments with //, multi-line comments with /* */, and a special triple-slash XML documentation format (///) placed above a member. XML doc comments support tags like <summary>, <param>, and <returns>, and are used by Visual Studio and other IDEs to power IntelliSense tooltips and can be compiled into full API documentation.
Arithmetic & Assignment Operators
C# provides the standard arithmetic operators for numeric computation: +, -, *, / for division, and % for the remainder. Integer division truncates any decimal part, just as in many C-family languages. Compound assignment operators (+=, -=, etc.) combine an operation with assignment, and increment/decrement operators (++, --) come in prefix and postfix forms that differ subtly in when the value updates relative to being used.