Comparing two predominant answers
(x%m + m)%m;
and
int r = x%m;
return r<0 ? r+m : r;
Nobody actually mentioned the fact that the first one may throw an OverflowException
while the second one won't. Even worse, with default unchecked context, the first answer may return the wrong answer (see mod(int.MaxValue - 1, int.MaxValue)
for example). So the second answer not only seems to be faster, but also more correct.