Sat. Aug 9th, 2025

Short-Circuiting with .NET C# example

In this post I will explain what short-circuiting is and show an example where the wrong implementation may cause problems.

Introduction

Short-circuiting is a concept in programming where the compiler will not evaluate next logical expressions if the previous part of the expression satisfies the condition

You can check additional documentation on Short-circuit on this link: Short circuit evaluation

Link to Short-Circuiting githab repository: ShortCircuitingApp

Application structure overview

Application is separated into few different layers for better visibility:

  • Model – Contains User class with basic properties
  • Repository – Which simulates database with mocked List of users and function for retrieval of user
  • Service – Where business logic for the retrieval of user is located
  • Program – For simple user interaction with console application

Example of wanted response

In console log we will return responses from two very similar functions.

GetUser

public (string? UserJson, string? Message) GetUser(int userId)
{
    var user = _userRepository.GetUserById(userId);
    if(user != null && user.IsActive)
    {
        string userJson = JsonSerializer.Serialize(user);
        return (userJson, "Valid user found");
    } else
    {
        return ("No user", "Valid user not found");
    }
}

GetUserBadImplementation

public (string? UserJson, string? Message) GetUserBadImplementation(int userId)
{
    var user = _userRepository.GetUserById(userId);
    if (user.IsActive && user != null)
    {
        string userJson = JsonSerializer.Serialize(user);
        return (userJson, "Valid user found");
    }
    else
    {
        return ("No user", "Valid user not found");
    }
}

Notice that only difference in implementation is in order of logical evaluation of the expression.

If we start a program and input the number between 1 and 6 we will get the response like this:

or we can enter string like “test” and we will get this response:

Both of these cases are expected and by just looking at them like this it seems that both functions are working the same.

Example of unwanted response

To demonstrate where the problem is, we are going to input the number greater than 6 which means that we are trying to fetch the user which does not exist and that we should technically get null for the value of the user object.

For the first function we got the correct response but for the second one we got the NullReferenceException. GetUserBadImplementation fails because it does not properly check for null before accessing properties on the user object. This is a direct consequence of not using short-circuiting effectively.

To avoid this ensure that:

  • Null checks are performed before accessing object properties or methods.
  • Logical short-circuiting is being used to avoid unnecessary evaluations and potential exceptions

You can clone repository by clicking on the Github icon:


By Denis