Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Well, your <em>explanation</em> isn't an explanation at all, it's an observation of behavior, which is different.</p> <p>If you want an explanation of what the difference is, then you need a piece of text <em>explaining</em> it. And the behavior explained can be ed with the code.</p> <p>The page linked to by Grimtron is good for detailing all the individual differences between a class and a struct, and pieces of it would serve as a <em>overview explanation</em>, in particular read the following items:</p> <ul> <li>Exists on stack or heap?</li> <li>Inheritance differences?</li> </ul> <p>But I wouldn't link to that page as an <em>explanation</em> for what the differences are. It's like trying to describe what a car is, and just listing up all the parts that make up a car. You still need to understand the big picture to understand what a car is, and such a list would not be able to give you that.</p> <p>In my mind, an explanation is something that tells you how something works, and then all the details follow naturally from that.</p> <p>For instance, if you understand the basic underlying principles behind a value type vs. a reference type, a lot of the details on that page makes sense, <em>if you think about it</em>.</p> <p>For instance, a value type (struct) is allocated where it is declared, inline, so to speak. It takes up stack space, or makes a class bigger in memory. A reference type, however, is a pointer, which has a fixed size, to <em>somewhere else in memory where the actual object is stored</em>.</p> <p>With the above explanation, the following details makes sense:</p> <ul> <li>A struct variable cannot be null (ie. it always takes up the necessary space)</li> <li>An object reference can be null (ie. the pointer can point to nothing)</li> <li>A struct does not add pressure to garbage collection (garbage collection works with the heap, which is where objects live in that <em>somewhere else</em> space)</li> <li>Always have a default constructor. Since you can declare any value-type variable (which is basically a kind of struct), without giving it a value, there must be some underlying magic that clears up that space (remember I said it took up space anyhow)</li> </ul> <p>Other things, like all the things related to inheritance, needs their own part in the explanation.</p> <p>And so on...</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload