By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,672 Members | 1,304 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,672 IT Pros & Developers. It's quick & easy.

Reference Types Performance MS Peeps please sound off!

P: n/a
First, is null considered a Value Type or a Reference Type.

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)

Please Help!
Nov 13 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
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
Nov 13 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.