Among .NET developers, confusion arises regarding the difference between reference types and value types so often that I decided it was worthwhile to write a short story explaining what makes these types different (and what doesn’t).
Over time, the People spread throughout the land. There became River People, and Valley People. Both the River People and the Valley People were People, of course; but the River People lived mostly by the River while the Valley People lived mostly in the Valley.
Of course, where these People lived did not truly matter. But there was a very interesting difference that evolved between the People, and this was in how they conveyed themselves.
When a River Person desired to convey himself, he would write his address on a note and float it along the river towards the place where he wanted it to go. Then when the note arrived at its destination, the recipient would lift it out of the river and see the Person’s address; from this, the recipient would know where to find him.
The Valley People, on the other hand, had developed a strange and different way to convey themselves. When a Valley Person wanted to be conveyed, he would copy himself—arms, legs, everything—and would send his copy wherever he wanted for it to go. Once the copied Valley Person had arrived at his destination, he could interact with those in the area directly.
Though the River People and the Valley People conveyed themselves in these different ways, being People, they could still communicate with one another without any difficulty. When a River Person had a message to deliver, he could communicate this message to a Valley Person just as easily as he could to another River Person. Likewise, a Valley Person had no more difficulty communicating his messages to a River Person than to a fellow Valley Person. Thus it could be said that People simply communicated with People; the distinction between the River and the Valley played no role in this.
And despite the fact that they called themselves River People and Valley People, in truth there were some belonging to one group who lived in the realm typically inhabitated by the others. In essence, the only real difference between the two Peoples was what has already been said: they conveyed themselves in different ways.
Now one night, a teacher and a pupil sat atop a hillside, overlooking the Land of Common. Looking down at the pupil, the teacher asked, “What is on your mind, young one? You look puzzled.”
The pupil frowned. “I am puzzled,” he said. “What I was just thinking was: how is it that when I talk to a Valley Person, he can understand me?” (The pupil and the teacher were themselves River People.)
“Why should he not understand you?” asked the teacher. “You are both People, are you not?”
The pupil consented: “We are both People, but… his kind live in the Valley! Our kind live by the River! We are… different.”
The teacher held up one finger. “Not so fast,” he said; “you know that not all Valley People live in the Valley. Some live with us, by the River.”
“Yes, yes,” the pupil agreed; “but how can I tell the difference?” He furrowed his brow in frustration.
“The real difference between a River Person and a Valley Person has nothing to do with where he lives,” the teacher reminded the pupil. “What makes us different is only how we convey ourselves. That is all. We are still all People, as we all come from a common ancestor.”
“It just seems strange to me,” the pupil protested, “that we can be the same when we live in different places and convey ourselves in different ways.”
This time the teacher nodded. “It might seem strange to you,” he allowed; “but you must not confuse yourself thinking about unrelated matters. We do have differences; but we can still communicate with one another because we are all People. Where a Person lives is immaterial in this regard.”
“We merely need to understand,” the teacher continued, “that when we are visited by a Valley Person, we are communicating with a copy; and when we are contacted by a River Person, we must go to his address and find him there. This is the true difference. After all, the Valley People could move out of the Valley and they would still convey themselves in their way. We could move away from the River and we would still convey ourselves our way.”
The teacher rose. “When you come to understand this, I think you will find that your questions disappear.”
The pupil nodded his head slowly, gazing off towards the horizon and pondering his teacher’s words.
In the above story, a Person represents an
object; the River People represent reference types, the Valley People represent value types, and questions like “How can value types inherit from
System.Object?” basically boil down to How is it that Valley People are People?
If you understood all that, congratulations! You understand the difference between reference types and value types.