Rats. I wrote a reply on Saturday, but evidently it was swallowed by
the Web.
Variables and constants are storage locations with names. A constant is a
variable with a continuous value.
Be careful. In C#, constants often do not have storage locations. They
can be (usually are) just compile-time constructs. For example, the
compiler will render
const int a = 5;
const int b = 6;
int c = a + b;
down to this:
int c = 11;
Variables do have storage locations; constants may not.
A "read only" variable is a constant.
Not exactly. The "readonly" keyword in C# guarantees that the variable
can be changed only in the class's constructor(s) and nowhere else.
However, if the "readonly" variable is a reference to an object on the
heap, the "readonly" keyword does _not_ guarantee that the object
itself cannot be changed outside the constructor, only that the
reference will never point to any other object than the one it was
pointing to when the constructor finished executing.
"readonly" built-in types (such as int and float) are in fact
invariable after the constructor terminates, and are a handy way to
create constants that require some complex calculations... constants
that can't simply be built at compile time.
The value of a variable can be numerical or alpha-numerical (i.e. a
string value).
A little simplistic, I think. From the lowest-level point of view,
variables just contain bit patterns that can be interpreted in various
ways. From a higher-level point of view, variables have an almost
infinite variation of types, as the .NET Framework class library
illustrates. As a trivial example, enumerated types don't fall into
either of these categories, and neither do structs that compose complex
value types.
Variables like int are called "intrinsic", or (=) "built-in" or (=) "primitive" types.
Primitive types are structs.
That's backward, to my way of thinking. Primitive types are not
structs; structs act in many ways like primitive (value) types. The two
have similar behaviour, but that doesn't imply an "is a" relationship,
in my mind, and especially not "primitive type is-a struct".
Which means that all variable in C# are objects.
Now, that's an interesting assertion. It all depends upon how you
define "object". I tend to refer to "objects" in C# as reference types,
but then it's true that in C# value types derive from Object and have
methods, including GetType(), so they have some properties of objects.
However, you can't inherit from them. So are they really full-fledged
"objects"? They certainly more object-like than primitive types in,
say, Java or C++.
The value of a variable is retrieved directly from the stack ("value types"), or indirectly
via a pointer ("reference types").
For local variables and method arguments, yes.
Pointers represent values, which themselves cannot be approached
directly. Because of this difference value types are more efficient.
Be careful here. Yes, value types are "more efficient" for many
operations, but this does _not_ mean that you should decide between
using a value type and using a reference type based on value types
being "more efficient". This is efficiency at a very low (I'd say even
"trivial") level. Real performance gains are usually had by looking at
efficiency at a far higher level of abstraction than this. In that
sense, the "most efficient" construct--value type or reference
type--for a job is the one that best fits the design. Deciding to use
value types becaues of some idea that they are generally "more
efficient" usually leads to tortured and--yes--inefficient code, in my
experience.
Pointers are stored on the stack, whereas the values they represent
are stored on the heap.
Again, yes, for reference types that are local variables or method
arguments.
A variable declared in an object is stored together (some authors say
"inline") with that object.
Yes. On the heap.
For example a system array refers to a contiguous block of memory on the heap,
large enough to hold the number and type of elements for which it was
declared. Its pointers are on the stack; its values are on the heap. But in
the case of an array of reference types, there are pointers on the stack -
to pointers on the heap - pointing to values on the heap.
Yes, keeping in mind that when you say "its pointers" above, you're
talking about the various references that your application is holding
to the array. Those are held on the stack (or, if they form part of an
object's state, on the heap with the other object state data).