ArgumentOutOfRangeException
C# Programming Language
Severity: ModerateWhat Does This Error Mean?
ArgumentOutOfRangeException means a value you passed to a method was outside the range that method accepts. For example: calling String.Substring(50) on a string that only has 10 characters. The error message usually tells you which parameter was invalid and what value it had. Fix it by validating your values before passing them to methods.
Affected Models
- .NET Framework
- .NET Core
- .NET 5+
- Unity (C# scripting)
- ASP.NET
Common Causes
- Calling String.Substring() with a starting position larger than the string length
- Passing a negative index to List.RemoveAt(), Insert(), or similar list methods
- Using DateTime with an invalid value — like month 13 or day 0
- Passing a page number or offset larger than the actual data size in pagination logic
- Calculating an index from user input or external data without validating the result first
How to Fix It
-
Read the error message. It usually states the parameter name and the value that caused the problem. Example: 'startIndex cannot be larger than length of string. (Parameter: startIndex)'
Write down the parameter name and the bad value. This tells you exactly what to look for in your code.
-
Find where that value was calculated or set. Check if it could ever exceed the valid range — especially when dealing with strings, lists, or dates.
Common pattern: an index is calculated based on data that varies. When the data is short or empty, the index becomes invalid.
-
Add a range check before calling the method. Clamp the value to the valid range or exit early if it is invalid.
Example for strings: if (index >= 0 && index < myString.Length) { use myString.Substring(index); }
-
For string operations, use Math.Min() to safely clamp the length. For example: myString.Substring(0, Math.Min(10, myString.Length))
Math.Min() picks the smaller of two numbers — this guarantees you never ask for more characters than the string has.
-
If the value comes from user input, validate it before use. Reject or correct invalid input early rather than letting it cause crashes deeper in the code.
Input validation at the boundary of your program prevents a whole class of range errors throughout the codebase.
When to Call a Professional
ArgumentOutOfRangeException is a code bug you can fix yourself. The error message names the parameter and often shows the invalid value. Trace back to where that value was calculated and add a range check. If values come from user input or an external API, always validate before using them.
Frequently Asked Questions
What is the difference between ArgumentOutOfRangeException and IndexOutOfRangeException?
Both mean a number was outside the valid range, but they happen in different contexts. IndexOutOfRangeException is thrown by the runtime when you access an array with a bad index. ArgumentOutOfRangeException is thrown by methods (including .NET built-in methods) when an argument value is outside acceptable bounds. If you are using square brackets on an array, it is IndexOutOfRange. If you are calling a method, it is ArgumentOutOfRange.
How do I handle this error when the value comes from user input?
Validate all user input before using it in calculations. Check that numbers are within expected minimums and maximums. Show the user a clear error message if their input is invalid, rather than letting it crash your program. This is called input validation — it is a core part of writing robust software.
Can I throw ArgumentOutOfRangeException in my own methods?
Yes — and you should, when appropriate. If your method has a parameter that must be within a specific range, check it at the top and throw if invalid. In .NET 8+, you can use ArgumentOutOfRangeException.ThrowIfNegative() and similar helpers for common cases. This gives callers a clear error message and makes your method easier to use correctly.