Dave and I have been doing alot of interviews lately (well, mostly Dave), and from this atmosphere of knowledge assessment I have started thinking about good .NET trivia questions. These aren’t questions we actually ask in the interview (quit trying to cheat) but instead questions that cover the same kind of things. It helps that I have been re-reading Richter’s book (along with my friend Jayme) and we have fun trying to come up with questions the other can’t answer.
I thought it might be fun to post some of these here… so here goes.
Given the following piece of code:
int x = 1; object.ReferenceEquals(x, x);
What will ReferenceEquals return (either true or false)
Scroll down the for answer
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
It will return false. Why? Well, ReferenceEquals compares two objects… and since object is a reference type and integers are value types each of the parameters are boxed separately when passed into the method. Since it is boxed into separate spots on the heap the reference addresses won’t be the same and the method returns false.
Confused? Check out these resource on reference types, value types, and the stack and the heap: