C# Iif Equivalent
C# Conditional Expressions
C# Programming Tips
C# Ternary Operator
Iif Function in C#

Iif equivalent in C

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

Visual Basic has IIf, but in C sharp the normal equivalent is the conditional operator, often called the ternary operator. The key difference is that C sharp chooses a language expression instead of a helper function. That design matters because it gives normal type checking, better readability, and short-circuit evaluation.

Use the Ternary Operator

The direct equivalent for most IIf cases is:

csharp
1using System;
2
3int score = 72;
4string result = score >= 50 ? "pass" : "fail";
5
6Console.WriteLine(result);

This reads as:

  • if the condition is true, use the first value
  • otherwise, use the second value

For simple inline decisions, this is the standard C sharp idiom.

Why It Is Better Than a Function-Style IIf

One important difference from the Visual Basic IIf history is evaluation behavior. The conditional operator in C sharp evaluates only the chosen branch.

csharp
1using System;
2
3int a = 10;
4int b = 0;
5
6string message = b != 0 ? (a / b).ToString() : "division skipped";
7Console.WriteLine(message);

Because the false branch is selected, the division is never attempted. This short-circuit behavior is one reason the operator is safer than an always-evaluate style helper.

Use It with Method Calls

The conditional operator works well when each branch calls a different method.

csharp
1using System;
2
3static string Small() => "small";
4static string Large() => "large";
5
6int value = 12;
7string label = value > 10 ? Large() : Small();
8
9Console.WriteLine(label);

Again, only one method is executed.

Keep Complex Branches Out of One Line

Although the ternary operator is concise, deeply nested expressions quickly become hard to read.

Readable:

csharp
string state = isAdmin ? "admin" : "user";

Usually not readable:

csharp
string state = isAdmin ? (isActive ? "active-admin" : "inactive-admin") : (isActive ? "active-user" : "inactive-user");

When the expression becomes hard to scan, switch to if and else.

Sometimes when developers ask for an IIf equivalent, they actually want one of these:

  • ternary operator for general conditionals
  • null-coalescing operator for fallback values
  • pattern matching for type-based decisions

Null fallback example:

csharp
string? input = null;
string output = input ?? "default";
Console.WriteLine(output);

That is not the same as a full conditional, but it solves a common case more clearly.

Make the Result Type Clear

Both branches of a ternary expression should be compatible types.

csharp
object value = true ? 123 : "text";
Console.WriteLine(value);

This works because both branches can be assigned to object. But if the types are unrelated and the target type is unclear, the compiler may reject the expression or infer a type you did not intend.

When in doubt, assign to an explicit variable type or refactor into if and else.

Prefer if and else for Statements

The conditional operator is an expression, not a block-structured control statement. If you need multiple actions, side effects, logging, or mutation across several lines, ordinary if and else is better.

csharp
1if (score >= 50)
2{
3    Console.WriteLine("pass");
4}
5else
6{
7    Console.WriteLine("fail");
8}

This is longer, but often clearer for real application logic.

Common Pitfalls

  • Treating the ternary operator as a replacement for every if and else.
  • Writing nested ternary expressions that are hard to read and review.
  • Forgetting that both branches need compatible types.
  • Confusing null-coalescing with a full conditional expression.
  • Assuming C sharp needs a function-style IIf helper at all.

Summary

  • The usual IIf equivalent in C sharp is the ternary operator.
  • The syntax is condition ? whenTrue : whenFalse.
  • Only the selected branch is evaluated.
  • Use it for simple inline expressions, not complex control flow.
  • Prefer if and else when readability matters more than brevity.

Course illustration
Course illustration

All Rights Reserved.