While reflecting with ILSpy i found this line of code in the Queue<T>.Enqueue(T item)-method:
if (this._size == this._array.Length)
{
int num = (int)((long)this._array.Length * 200L / 100L);
if (num < this._array.Length + 4)
{
num = this._array.Length + 4;
}
this.SetCapacity(num);
}
I’m just wondering why somebody would do this? I think it’s some kind of a integer overflow check, but why multiply first with 200L and then divide by 100L?
Might this have been a issue with earlier compilers?
Usually things first multiplied then divided by 100 are percentage calculations – Perhaps there was some
const XxxPercentage = 200or something like that in the original code. The compiler does not seem to optimize the* 200 / 100to* 2.This code sets the capacity to twice its size – but if twice its size would be smaller than the original size + 4, use that instead.
The reason it is converted to long probably is because if you multiply an integer by the “200 percent” it would overflow.