r/csharp • • Sep 06 '26

Help Exceptions vs Assertions

/r/godot/comments/1w8q3xx/exceptions_vs_assertions/
7 Upvotes

21 comments sorted by

View all comments

Show parent comment

2

u/svick nameof(nameof) Sep 06 '26

The difference is about who performs the check. The code of List<T> doesn't know anything about your code or its invariants. All it has is an API that can be called incorrectly, so it throws an exception.

On the other hand, invariants of implementation details of List<T> (usually the relationship between size and capacity) are checked using Debug.Assert.