programming
error handling
index out of bounds
zero-based indexing
debugging

Index zero based must be greater than or equal to zero

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

Introduction

The error "Index (zero based) must be greater than or equal to zero and less than the size of the argument list" is a .NET FormatException (or ArgumentOutOfRangeException) that occurs when using string.Format(), composite formatting, or collection indexing with an invalid index. It means you are referencing a placeholder like {2} but only provided 2 arguments (indices 0 and 1), or you are accessing a negative index in a collection.

The Error in String Formatting

The most common trigger is a mismatch between placeholders and arguments in string.Format():

csharp
1// WRONG — {2} requires 3 arguments, but only 2 provided
2string result = string.Format("Name: {0}, Age: {1}, City: {2}", "Alice", 30);
3// FormatException: Index (zero based) must be greater than or equal to zero
4
5// FIX — provide all required arguments
6string result = string.Format("Name: {0}, Age: {1}, City: {2}", "Alice", 30, "NYC");

Why It Happens

string.Format uses zero-based indexing for placeholders:

  • {0} → first argument
  • {1} → second argument
  • {2} → third argument

If the highest placeholder index is {N}, you need at least N + 1 arguments.

Common Causes

Cause 1: Missing Arguments

csharp
1// WRONG — placeholder {1} but only 1 argument
2string msg = string.Format("Hello {0}, welcome to {1}", username);
3
4// FIX
5string msg = string.Format("Hello {0}, welcome to {1}", username, siteName);

Cause 2: Wrong Placeholder Index

csharp
1// WRONG — skipped {1}, jumped to {2}
2string msg = string.Format("{0} scored {2} points", name, score);
3
4// FIX — use consecutive indices
5string msg = string.Format("{0} scored {1} points", name, score);

Cause 3: Curly Braces in Text

csharp
1// WRONG — {JSON} is interpreted as placeholder index
2string msg = string.Format("Format: {JSON}", value);
3// FormatException
4
5// FIX — escape curly braces by doubling them
6string msg = string.Format("Format: {{JSON}} = {0}", value);

Cause 4: Console.WriteLine and Similar Methods

csharp
1// WRONG
2Console.WriteLine("User {0} logged in from {1} at {2}", user, ip);
3// Missing third argument for {2}
4
5// FIX
6Console.WriteLine("User {0} logged in from {1} at {2}", user, ip, time);

Cause 5: String Interpolation Migration

When converting from string.Format to interpolated strings, leftover placeholders cause issues:

csharp
1// Old code
2string msg = string.Format("Order {0}: {1} items, total ${2:F2}", orderId, count, total);
3
4// Correct migration to interpolation
5string msg = $"Order {orderId}: {count} items, total ${total:F2}";
6
7// WRONG migration — mixed syntax
8string msg = $"Order {0}: {count} items";  // {0} is literal "0" in interpolation

The Error in Collection Indexing

The same concept applies to list/array access:

csharp
1var list = new List<string> { "a", "b", "c" };
2
3// WRONG — index -1 is invalid
4string item = list[-1];  // ArgumentOutOfRangeException
5
6// WRONG — index 3 is out of range (valid: 0, 1, 2)
7string item = list[3];  // ArgumentOutOfRangeException
8
9// FIX — validate index
10if (index >= 0 && index < list.Count)
11{
12    string item = list[index];
13}

Prevention with String Interpolation

Modern C# (6.0+) eliminates most formatting errors with string interpolation:

csharp
1string name = "Alice";
2int age = 30;
3string city = "NYC";
4
5// String interpolation — no index mismatch possible
6string result = $"Name: {name}, Age: {age}, City: {city}";
7
8// With formatting
9decimal price = 19.99m;
10string formatted = $"Price: {price:C2}";  // Price: $19.99

Debugging Tips

csharp
1// Count your placeholders vs arguments
2string template = "A: {0}, B: {1}, C: {2}";  // 3 placeholders
3// Need: string.Format(template, arg0, arg1, arg2)  // 3 arguments
4
5// Use a helper to validate at runtime
6static string SafeFormat(string format, params object[] args)
7{
8    try
9    {
10        return string.Format(format, args);
11    }
12    catch (FormatException ex)
13    {
14        return $"FORMAT ERROR: {ex.Message} | Template: {format} | Args: {args.Length}";
15    }
16}

Common Pitfalls

  • Dynamic format strings: When the format string comes from a database, config file, or resource file, the argument count may not match. Always validate dynamic format strings.
  • Localization: Translated strings may have different placeholder counts than the original. Ensure all translations maintain the same placeholders.
  • Logging frameworks: Libraries like NLog and Serilog use similar {0} syntax. A mismatch between template placeholders and arguments causes the same error.
  • Off-by-one: Forgetting that indices start at 0, not 1. {1} is the second argument, not the first.
  • Escaped braces: To include a literal { or } in a format string, double it: {{ or }}. A single { without a valid index triggers a FormatException.

Summary

  • The error occurs when a format placeholder references an index that does not have a corresponding argument
  • {0} is zero-based — {N} requires at least N + 1 arguments
  • Use string interpolation ($"...") in C# 6+ to avoid index mismatch entirely
  • Escape literal curly braces with {{ and }}
  • Validate format strings from external sources (databases, config files, translations)
  • For collection indexing, always check index >= 0 && index < count before access

Related reading
Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

All Rights Reserved.