3

I'm reading this article on MSDN about the new operator:

https://msdn.microsoft.com/en-us/library/fa0ab757(v=vs.120).aspx

I do not understand this paragraph:

Value-type objects such as structs are created on the stack, while reference-type objects such as classes are created on the heap. Both types of objects are destroyed automatically, but objects based on value types are destroyed when they go out of scope, whereas objects based on reference types are destroyed at an unspecified time after the last reference to them is removed.

What exactly does the bold text mean? What is an "object based on value/reference types"? Is that an object which only contains value members / only reference members?

Edit I don't think the 'What is the heap / stack' is an appropriate duplicate. While the selected answer in that linked thread is highly informative, I loosely understand the differences between the two -- as much as "the stack stores value types, the heap stores reference types". But I was under the impression that a 'value type' would be things like int or char; I didn't realize a struct is included in that, or that a struct can be defined as "an object based on value types".

sab669
  • 3,984
  • 8
  • 38
  • 75
  • 2
    They just mean `struct` and `class`, respectively. – Jonathon Reinhart Feb 09 '17 at 12:59
  • Possible duplicate of [What and where are the stack and heap?](http://stackoverflow.com/questions/79923/what-and-where-are-the-stack-and-heap) – Igor Feb 09 '17 at 13:00
  • @Igor I disagree with your duplicate vote. This has to do with .NET types that happen to be stored in different locations. There's nothing on that page that answers his question. – Jonathon Reinhart Feb 09 '17 at 13:01
  • @JonathonReinhart That seems like a much simpler answer than I was expecting, but now I realize I know / learned very little about `struct`s so I guess I'm going to read that article now... I think that's sufficient to post as an answer for me to Accept, as it does definitely answer my question. – sab669 Feb 09 '17 at 13:01
  • 4
    @sab669 - I wouldn't read that article too hard. It focusses on the wrong things. (Stack/Heap/Lifetime). I'd instead recommend Eric Lippert's [The Truth about Value Types](https://blogs.msdn.microsoft.com/ericlippert/2010/09/30/the-truth-about-value-types/) – Damien_The_Unbeliever Feb 09 '17 at 13:07
  • @Damien_The_Unbeliever I'll check it out, but I'm not sure it's related to what set me down this path. I just recently learned you can't return `null` in a constructor in .NET so I was trying to figure out exactly *what* the `new` operator does in order to understand *why* you can't return `null`. I understand constructors don't specify a return type, but I kind of assumed that it was some sort of syntactical sugar to basically say, "By not specifying a return type, it automatically converts this to return an instance of this type; whatever that may be" – sab669 Feb 09 '17 at 13:26
  • _Value-type objects such as structs are created on the stack_ is simply not true. Do read the article by Eric Lippert. – H H Feb 09 '17 at 13:34
  • @HenkHolterman Could you elaborate? – sab669 Feb 09 '17 at 13:35
  • Why would I? This has been asked and answered many times. You now have some links and some new names (= search terms). – H H Feb 09 '17 at 13:36

1 Answers1

3

You're over-thinking it:

  • "object based on value types" generally means struct
  • "object based on reference types" generally means class

I honestly don't know why they changed their terminology mid-paragraph; I think this would have been clearer and just as correct:

Value-type objects such as structs are created on the stack, while reference-type objects such as classes are created on the heap. Both types of objects are destroyed automatically, but value-type objects are destroyed when they go out of scope, whereas reference-type objects are destroyed at an unspecified time after the last reference to them is removed.

Perhaps there are some corner-cases that this explanation doesn't cover, but it should be good enough for most cases.

Selman Genç
  • 100,147
  • 13
  • 119
  • 184
Jonathon Reinhart
  • 132,704
  • 33
  • 254
  • 328
  • 2
    The main reason they don't say `class` and `struct` specifically is that both actually have other "members" - value types also include e.g. primitive types and enums, for example, while reference types also include e.g. interfaces (yes, even interfaces implemented by a value type). The tricky part is that while all .NET types are nominally derived from `System.Object`, it isn't *really* true - a runtime `int` is not an `object`, though it can be *cast* to an `object` (and that boxed value is no longer a value type). – Luaan Feb 09 '17 at 13:12
  • Right - I'm just not sure why they changed terminology mid-paragraph from saying "Value-type objects" to "objects based on value-types". Typical Microsoft I suppose; more verbose but less clear. – Jonathon Reinhart Feb 09 '17 at 13:14
  • Well, it's all pretty complicated, and the people writing the documentation are neither the most knowledgeable, nor the ones best at explaining. It isn't necessarily even written by the same guy :) The accent of the paragraph is clearly on making it clear that while all the managed objects will be cleaned-up eventually, some objects might take longer than others - it's not an authoritative document on the inner workings of .NET (indeed, all of this is implementation specific, not part of the CLR specification), just a quick and dirty explanation of some common scenarios. – Luaan Feb 09 '17 at 13:40