2

I know there are many many questions covering this topic and I have read them all. But I'm still left wondering this:

If an object is a reference type and all value types are derived from object, then is it accurate to say that a value type such as int is also a reference type? Specifically, if I wrote:

int i = 10;

Is i holding the value of 10, or is it an object holding a reference to the value of 10?

Or is it more accurate to say that a value type can be a reference type through the process of boxing.

  • You shouldn't just read the questions, you should also read the answers. :) Also read the [links](https://blogs.msdn.microsoft.com/ericlippert/2009/08/06/not-everything-derives-from-object/) that people provided in the answers. I don't see how this isn't a duplicate of the questions you linked to in your first sentence. – Rufus L Nov 01 '17 at 06:10
  • I did read the answers. I actually spent several hours reading the answers. I just needed more clarification and I guess this wasn’t the right forum. Although I did get that clarification below. – user5596939 Nov 01 '17 at 13:02

1 Answers1

2

is it accurate to say that a value type such as int is also a reference type? …Or is it more accurate to say that a value type can be a reference type through the process of boxing.

Neither is accurate. int is a value type, period. It is not "also a reference type". And when boxed, it's not magically changed to be a reference type. The System.Object that contains it is a reference type, but inside is still the value of the value type int.

Is i holding the value of 10, or is it an object holding a reference to the value of 10?

See above. i has the type int, which is a value type, and so the variable contains the value you've assigned to it, 10.

See also What is the difference between a reference type and value type in c#?

Peter Duniho
  • 68,759
  • 7
  • 102
  • 136
  • +1. You might also want to add the link to [ValueType](https://learn.microsoft.com/en-us/dotnet/api/system.valuetype?view=netframework-4.7.1) – Zohar Peled Nov 01 '17 at 05:55
  • @PeterDuniho "The System.Object that contains it is a reference type, but inside is still the value of the value type int" - this is exactly what I needed to know. Thank you for clarifying. – user5596939 Nov 01 '17 at 14:27