In LINQ version short circuits, your loop is not. By this, I mean that when you determine that a particular integer is actually a factor, the LINQ code stops, returns it, and then jumps. Your code continues the loop until it completes.
If you change the for to include this short circuit, you should see a similar performance:
int NextPrimeB(int from) { while(true) { n++; for (int i = 2; i <= (int)Math.Sqrt(n); i++) { if (n % i == 0) return n;; } } }
Servy source share