Suppose you have this code:
static void Send(byte[] buffer, int offset, int count)
{
while (count > 0)
{
int size = sock.Send(buffer, offset, count);
if (size == 0) throw new EndOfStreamException();
offset += size;
count -= size;
}
}
vs.
static void Send(byte[] buffer, int offset, int count)
{
int size;
while (count > 0)
{
size = sock.Send(buffer, offset, count);
if (size == 0) throw new EndOfStreamException();
offset += size;
count -= size;
}
}
My teacher told me the second one would be more efficient, but is that true?
I did some testing and compared the assembly code and it was equal.
But will this always be the case?
- What if instead of a primitive type you used a reference type? (assuming you don't allocate)
- What about using multiple variables?
- Will there ever be differences between the two possibilities?
Thanks in advance for any answers, I apologize if this has been asked before but I didn't really know how to call it.