A guard clause handles invalid input before the main work continues.

guard clause A guard clause returns early when a value should not continue through a method.

Guard Clauses

quantity
GuardClauses.cs
Replay: real traced execution (multi-file project)
using System;

class Program
{
    static int ShippingCost(int quantity)
    {
        if (quantity <= 0)
        {
            return 0;
        }

        return 5 + quantity;
    }

    static void Main()
    {
        int quantity = 3;
        int cost = ShippingCost(quantity);

        Console.WriteLine($"quantity={quantity}");
        Console.WriteLine($"cost={cost}");
    }
}
using System;

class Program
{
    static int ShippingCost(int quantity)
    {
        if (quantity <= 0)
        {
            return 0;
        }

        return 5 + quantity;
    }

    static void Main()
    {
        int quantity = 0;
        int cost = ShippingCost(quantity);

        Console.WriteLine($"quantity={quantity}");
        Console.WriteLine($"cost={cost}");
    }
}
using System;

class Program
{
    static int ShippingCost(int quantity)
    {
        if (quantity <= 0)
        {
            return 0;
        }

        return 5 + quantity;
    }

    static void Main()
    {
        int quantity = 8;
        int cost = ShippingCost(quantity);

        Console.WriteLine($"quantity={quantity}");
        Console.WriteLine($"cost={cost}");
    }
}
  1. quantity ← 3

    15static void Main()16{17    int quantity→ 3 = 3; //@quantity=0, 818    int cost = ShippingCost(quantity3);
  2. static int ShippingCost(int quantity)

    4{5    static int ShippingCost(int quantity3)6    {7        if (quantity <= 0)8        {9            return 0;10        }1112        return 5 + quantity3;13    }
  3. cost ← 8

    17    int quantity = 3; //@quantity=0, 818    int cost→ 8 = ShippingCost(quantity3);1920    Console.WriteLine($"quantity={quantity3}");21    Console.WriteLine($"cost={cost8}");22}
    outputquantity=3
    cost=8
  1. quantity ← 0

    15static void Main()16{17    int quantity→ 0 = 0;18    int cost = ShippingCost(quantity0);
  2. static int ShippingCost(int quantity)

    4{5    static int ShippingCost(int quantity0)6    {7        if (quantity <= 0)
  3. if (quantity <= 0)

    6{7    if (quantity0 <= 0)8    {9        return 0;10    }
  4. cost ← 0

    17    int quantity = 0;18    int cost→ 0 = ShippingCost(quantity0);1920    Console.WriteLine($"quantity={quantity0}");21    Console.WriteLine($"cost={cost0}");22}
    outputquantity=0
    cost=0
  1. quantity ← 8

    15static void Main()16{17    int quantity→ 8 = 8;18    int cost = ShippingCost(quantity8);
  2. static int ShippingCost(int quantity)

    4{5    static int ShippingCost(int quantity8)6    {7        if (quantity <= 0)8        {9            return 0;10        }1112        return 5 + quantity8;13    }
  3. cost ← 13

    17    int quantity = 8;18    int cost→ 13 = ShippingCost(quantity8);1920    Console.WriteLine($"quantity={quantity8}");21    Console.WriteLine($"cost={cost13}");22}
    outputquantity=8
    cost=13