Quizical <Co************@yahoo.com> wrote:
First, is null considered a Value Type or a Reference Type.
null is a reference.
I have a question about performance and memory. Does the performance
change or the amount of memory that is consumed when you have a
difference in flow control statements as follows:
//Arbitrary Reference Type
Mailmessage mm = new Mailmessage();
//A
if (null != mm)
{
Console.WriteLine(mm.ToString());
}
Or
//B
if (mm != null)
{
Console.WriteLine(mm.ToString());
}
I have been told that A performs better since the CLR does not have to
make a copy of the value null, I think it doesn't matter since both
values must be checked regardless. (I also think this reads like crap)
Neither version uses null - the test compiles to
ldloc.0 // Put the value of mm on the stack
brfalse.s [whatever] // Skip the block if the topmost stack value is 0
Given that the IL is the same in both cases, the JITted code should be
the same too.
Oh, and I agree that B reads better too - and that, to me, would be far
more important than any tiny performance gains which could be had if A
were in fact slightly different. Micro-optimisations like this attempts
to achieve should not be used unless that section of code is *proven*
to be a bottleneck.
--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too