Pages

Topics

Tools

↑↓navigate↵openescclose
All guides

C# If Statement: Syntax and Real-World Usage

Learn the C# if statement syntax, how to use else and else if branches, and how to avoid common pitfalls when writing conditionals in real projects.

~5 min readC#
#C# basics#conditional logic#control flow#code branching
Illustration of a decision branch in C# showing a boolean condition guiding code flow along two paths.

The if statement in C# is the most direct way to branch execution based on a boolean condition. In its simplest form, it evaluates an expression and runs a block of code only when that expression is true. Consider a method that returns a discount based on order size:

csharp
public decimal CalculateDiscount(int quantity) { decimal discount = 0m; if (quantity >= 100) { discount = 0.10m; } return discount; }

The condition quantity >= 100 is a boolean expression. If it evaluates to true, the block assigns 10% to discount. If it is false, the block is skipped entirely. This is the foundation of all conditional logic in C#.

Adding else and else if Branches

Real conditions usually need more than one outcome. The else keyword provides an alternative path when the initial condition is false:

csharp
if (quantity >= 100) { discount = 0.10m; } else { discount = 0.05m; }

When you have multiple branches to choose among, chain conditions with else if:

csharp
if (quantity >= 200) { discount = 0.15m; } else if (quantity >= 100) { discount = 0.10m; } else if (quantity >= 50) { discount = 0.05m; } else { discount = 0m; }

Order matters. The conditions are evaluated top-down, and execution stops at the first true one. If you placed quantity >= 50 before quantity >= 100, a quantity of 150 would incorrectly match the 5% branch. Always arrange conditions from most restrictive to least restrictive, or use explicit boundaries.

Braces and Single-Statement Form

C# allows you to omit braces when a branch contains only one statement:

csharp
if (quantity >= 100) discount = 0.10m; else discount = 0.05m;

This works, but it is risky. A common refactor accidentally changes which statements belong to which branch:

csharp
if (quantity >= 100) discount = 0.10m; ApplyLoyaltyBonus(); // Always runs! else discount = 0.05m; // Syntax error: else without matching if

The second statement is not part of the if block because only discount = 0.10m; is. The ApplyLoyaltyBonus() call always runs, and the else no longer has a matching if. Always using braces removes this ambiguity and makes the intended structure explicit.

Boolean Expressions and Common Mistakes

An if condition must be a boolean expression. Unlike some languages, C# does not implicitly convert integers or objects to bool. The following will not compile:

csharp
int count = 0; if (count) // Compiler error: cannot convert int to bool { // ... }

You must write an explicit comparison:

csharp
if (count != 0)

A frequent real-world bug is using a single = instead of == for equality:

csharp
if (count = 10) // Assignment, not comparison; compile error because int is not bool

In C#, this is caught at compile time because assignment returns an int, not a bool. This differs from languages that allow accidental assignment in conditions, but it is still a mistake worth avoiding.

Another common issue is confusing & and && in conditions. The single & performs bitwise AND on integers; when applied to booleans it is a logical AND that evaluates both sides. The short-circuit && skips the right operand if the left operand is false. In practice, short-circuiting matters for both performance and correctness:

csharp
if (items != null && items.Length > 0) { // Safe to access items.Length }

Using & here would evaluate items.Length even when items is null, causing a NullReferenceException. Always use && (and ||) unless you specifically need both sides evaluated (for example, because the right side has a required side effect).

Nested If and Early Return

Nesting if statements is sometimes necessary, but deep nesting quickly becomes hard to follow. Consider this data validation pattern:

csharp
if (order != null) { if (order.Items.Count > 0) { if (order.Customer.IsActive) { // Process order } } }

Each level adds cognitive overhead. In many methods, early returns reduce nesting and improve readability:

csharp
if (order == null) return; if (order.Items.Count == 0) return; if (!order.Customer.IsActive) return; // Process order

The early-return version keeps each condition on its own line and avoids indentation pyramids. Use this pattern when a method has multiple independent preconditions. It also makes the guard conditions explicit to future maintainers.

Short-Circuiting and Side Effects

Short-circuiting does not only prevent null-reference errors—it also affects behavior when conditions have side effects. The right-hand operand is not evaluated if the left-hand operand already determines the result:

csharp
if (IsValid(input) && SaveToDatabase(input)) { // ... }

Here SaveToDatabase only runs when IsValid returns true. That is a deliberate control-flow decision. If you need both calls to run regardless of the first result, call them as separate statements instead of combining them in one condition. Do not rely on the evaluation order without understanding which operand will be skipped.

When a switch Might Be Cleaner

An if-else chain that only compares one variable against several constant values is often better replaced with a switch expression. In C# 9 or later, relational patterns make this concise. For example:

csharp
string category = quantity switch { >= 1000 => "Wholesale", >= 100 => "Large", >= 10 => "Standard", _ => "Small" };

The switch expression is concise and guarantees that only one branch applies, similar to if-else but with a more declarative shape. Use it when the condition is a single value and the branches are straightforward constants or relational patterns. If your conditions compare multiple independent values or call methods, stick with if-else.

Performance Considerations in Hot Paths

In most business applications, if statement costs are negligible because the condition evaluates quickly. However, in tight loops or frequently called methods, avoid placing expensive method calls inside the condition if they are not necessary. For example:

csharp
if (cache.Get(key) != null && cache.Get(key).Value > threshold)

Because && short-circuits, the second lookup only happens when the first call returns non-null, but in that common case the cache is accessed twice. Store the result in a local variable to avoid the repeated lookup and to reuse the same object:

csharp
var cached = cache.Get(key); if (cached != null && cached.Value > threshold)

This is a minor optimization, but it also prevents repeated side effects and avoids relying on the cache returning the same object each call. In high-frequency paths, such micro-optimizations can add up, but always measure before changing for performance reasons. The primary benefit here is correctness.

Edge Cases: Equality with Floating-Point Values

When comparing double or float values, using == can produce subtly wrong results due to rounding. For example:

csharp
double x = 0.1 + 0.2; if (x == 0.3) // Often false because x is 0.30000000000000004

Instead, compare against a tolerance:

csharp
if (Math.Abs(x - 0.3) < 1e-9)

This is relevant when the condition depends on computed floating-point values. decimal stores base-10 values instead of binary fractions, so it avoids the 0.1 + 0.2 rounding problem for common monetary calculations. Choose the appropriate type for your domain; for monetary calculations, prefer decimal over double.

Conditional Access and Null Checks

A frequent pattern in modern C# is combining the if statement with null-conditional operators to avoid verbose null checks:

csharp
if (customer?.Address?.City == "Seattle") { // ... }

This single condition safely navigates the object graph. If any link is null, the whole expression evaluates to null, which does not equal "Seattle", so the block is skipped. This is much shorter than nested ifs and clearly expresses the intent. Use it when you need to check a value deep inside a hierarchy without worrying about intermediate nulls.

Maintainability: Keep Conditions Readable

Complex conditions hurt readability. Extract them into well-named methods:

csharp
if (order.IsEligibleForExpeditedShipping()) { // ... } private bool IsEligibleForExpeditedShipping() { return OrderTotal > 500 && CountryCode == "US" && !IsHoliday; }

The calling code reads like English, and the logic lives in one place. This also makes unit testing easier because the condition is isolated. Avoid inlining many compound conditions directly inside an if unless they are trivial.

A related tip: avoid negated conditions where possible. Prefer if (customer.IsActive) over if (!customer.IsInactive). Double negatives are hard to parse. If you must use negation, consider renaming the property or method to express the positive concept.

Try matching browser tools

All tools

Small utilities that fit C# work — they run entirely in this browser tab, so nothing you paste leaves your device.

All C# guides

More in C#

All C# guides

The guides filed next to this one in C#, in library order.