Is it more efficient to perform a range check by casting to uint instead of checking for negative values?
问题 I stumbled upon this piece of code in .NET's List source code: // Following trick can reduce the range check by one if ((uint) index >= (uint)_size) { ThrowHelper.ThrowArgumentOutOfRangeException(); } Apparently this is more efficient (?) than if (index < 0 || index >= _size) I am curious about the rationale behind the trick. Is a single branch instruction really more expensive than two conversions to uint ? Or is there some other optimization going on that will make this code faster than an