R
Raj
What is the difference between nullable class and nullable<> structure?
Thank you
Regards
Raj
Thank you
Regards
Raj
Raj said:What is the difference between nullable class and nullable<> structure?
Thank you
Regards
Raj
Raj said:What is the difference between nullable class and nullable<> structure?
Thank you
Regards
Raj
Classes are reference types and are allocated onto the heap with a
variable
pointing to them from the stack.
As such you can indicate that you want to
"de-reference" your object, by setting the variable to null:
StreamReader sr = New StreamReader("file path");
sr = null;
Now, the StreamReader will still be in memory, but the "sr" variable no
longer points to it.
A structure is a value type and is allocated on the stack.
Structures
cannot have a null value by definition so this wouldn't work:
Short x = 10;
x = null;
A value type *must* have a value - - it cannot be null. The C# compiler
will either assign a default value to a value type or expect you to do
it,
but either way a value type must have a value.
.NET Generics helps you work as if you have a value type that can, in
fact,
be null by creating a special class that holds value types, but because
it's
a class, null can be used:
nullable<Short>
Peter Duniho said:"Heap" and "stack" have very little to do with "reference type" and "value
type". It's true that no reference types are ever allocated on the
stack. But most instances of value types are also not allocated on the
stack. And it's definitely not true that reference types always have "a
variable pointing to them from the stack". Most often, instances of
reference types are referenced only by member variables of instances of
other types.
Any discussion of reference types and value types that begins by
mentioning the heap and the stack is, at a minimum, bound to fall short of
precision, and at worst will be extremely misleading.
Please don't do that.
The word "dereference" has a very specific meaning in programming: to
access a data structure via a pointer (or "reference") to the data
structure. Please do not use the same word to mean something else.
Again, it will only confuse the discussion. (For example, see
http://en.wikipedia.org/wiki/Dereference)
A structure is a value type, but it is absolutely not true that all
structures (or value types generally) are allocated on the stack. Please
don't say that.
True. But to be clear, for a _local_ variable, the compiler will never
assign a default value implicitly. All local variables are required to be
explicitly assigned by the code before use.
For the purposes of explaining why reference types can be null and value
types can't the explanation is sufficient.
Please don't tell me how I should explain my own point.
As such you can indicate that you want to
"de-reference" your object, by setting the variable to null: [...]
The word "dereference" has a very specific meaning in programming: to
access a data structure via a pointer (or "reference") to the data
structure. [...]
I know exactly how the term is used and my use of it is perfectly
correct.
Again, it is not for you to police other people's explanations.
Again, the explanation is sound and you should not feel that you need to
ask
others to "please" change the way they explain things to suit your
preferences.
I have not said that all value types are always allocated on
the stack.
Peter Duniho said:No, it's not. Your statement makes the claim that an instance of a
reference type is "pointed to...from the stack". This is not a
universally true statement. Nor is your later claim that all structures
are allocated on the stack.
You may explain your point any way you like, as long as you state only
true statements.
Once you fail to do that, you should not be surprised to be corrected,
and you have no standing to ask others not to correct you.
As such you can indicate that you want to
"de-reference" your object, by setting the variable to null: [...]
The word "dereference" has a very specific meaning in programming: to
access a data structure via a pointer (or "reference") to the data
structure. [...]
I know exactly how the term is used and my use of it is perfectly
correct.
Again, it is not for you to police other people's explanations.
You can use the word however you like. But do not be surprised when
people don't have any idea what you're talking about. Your usage is
absolutely non-standard, in spite of your opinion that it's "perfectly
correct". There isn't a single mainstream programming reference that uses
the term "dereference" to mean "set to null".
And again, if you use it in this non-standard way, you should not be
surprised to be corrected, nor do you have any standing to ask others not
to correct you.
I have written "please" because I know from past experience that you have
a very difficult time accepting any sort of disagreement or correction,
even when your statements are plainly wrong. It's an attempt to be gentle
about my corrections, so that perhaps you can be more accommodating.
But the fact is, your statements conflating the stack and value types are
simply wrong. Period. Your explanation is quite far from being "sound".
If you prefer that I stop using the word "please", so be it. I won't
waste any more time trying to be polite on this matter.
This isn't about "my preferences". It's about what is factually and
demonstrably correct. It's true, it's my preference that people not make
false statements. But my goal here is not to express that preference, but
rather to correct those false statements. I attempted to be kind about
it, so as to avoid you getting your hackles up, but obviously I failed in
that regard. No matter...that wasn't my primary goal. My primary goal
was to get the correct facts documented as part of this thread, and I've
done that.
You wrote "a structure is a value type and is allocated on the stack".
How else is anyone to interpret that statement, than to mean "all
structures are value types, and thus all structures are allocated on the
stack, just as all value types are"?
An unqualified statement regarding "a structure" is semantically the same
as being a statement about "all structures", and the phrasing of the
statement clearly describes an (incorrect) assertion that structures are
always allocated on the stack as being by virtue of structures being value
types, thus meaning that all value types are always allocated on the
stack.
At a minimum, you have clearly and unequivocally stated that "all
structures are always allocated on the stack", which is wrong, even if you
didn't mean to include all value types in the statement.
Raj said:What is the difference between nullable class and nullable<> structure?
Thank you
Regards
Raj
Any discussion of reference types and value types that begins by
mentioning the heap and the stack is, at a minimum, bound to fall
short of precision, and at worst will be extremely misleading.
Excellent stuff, as is usual with Eric's writing...
Gregory said:[...]
As for the stack and heap discussion, er fight, here: When we are first
taught the atomic structure, the solar system is often used as an
analogy, without disclaimers. It helps the young student understand
electrons and orbits.
Technically, however, it is a flawed analogy,
which is learned later on. In the same boat, using stack and heap as a
general way of describing value and reference types, especially to
junior devs, does not bother me as much as it might some. It helps wrap
the mind around the concept, even if it is not true in all conditions,
just as undestanding you are working with the value (value type) versus
a reference to the "value" works on a basic level. I am sure someone
here might disagree, which is their prerogative. ;-)
Gregory said:[...]
The heap, in general, is open memory, while the stack is program memory.
Now, anyone who has programmed for a good length of time will note that
this statement is not absolutely 100% correct and possibly even
misleading, but it is very useful when explaining why a person who
writes only structures (for speed?) and no classes is more apt to end up
with an out of stack memory condition in his program because he heard
that structures are faster.
Then we have the statement that structures are faster. This is true in
general, but we also know that it is not always true and it is a bad
practice to only code in structures.
My point here is I don't see that Scott's statements are absolutely
dangerous, but would agree, as a compromise, that the statements should
have a disclaimer.
I often add disclaimers to my statements so we can avoid these
"technically you are incorrect" type of statements when the user wants
to know how to prevent his ice cream from melting and I don't want to
explain the complete nature of refrigeration. ;-)
Peter Duniho said:Gregory said:[...]
As for the stack and heap discussion, er fight, here: When we are first
taught the atomic structure, the solar system is often used as an
analogy, without disclaimers. It helps the young student understand
electrons and orbits.
In what way? If the student already understand planetary orbital
mechanics, what knowledge there helps them understand atomic structure?
Are electrons kept in their orbits by gravity? Do they follow regular
elliptical paths? Are some electrons larger or smaller than others? Do
some electronics take more or less time to complete an orbit than others?
Do the protons and neutrons in the nucleus of an atom orbit each other,
again kept in proximity by gravity, but from collision by velocity?
I won't dispute that some teachers introduce the solar system as an
analogy for atomic structure. However, I will certainly debate the claim
that doing so is actually helpful to the students. There's practically
nothing about the analogy that helps a student better-comprehend atomic
structure.
Technically, however, it is a flawed analogy,
which is learned later on. In the same boat, using stack and heap as a
general way of describing value and reference types, especially to
junior devs, does not bother me as much as it might some. It helps wrap
the mind around the concept, even if it is not true in all conditions,
It's not true in _any_ conditions. The things that make value types
different from reference types and interesting as a particular kind of
data structure don't have anything to do with the stack. It's simply a
convenience of purpose that value types _sometimes_ wind up on the stack.
Knowing that value types occasionally wind up actually stored on the stack
doesn't provide any insight whatsoever into the things that make value
types useful or unique.
just as undestanding you are working with the value (value type) versus
a reference to the "value" works on a basic level. I am sure someone
here might disagree, which is their prerogative. ;-)
It is a well-established educational principle that misdirecting the
student in this fashion is counter-productive. Using a false analogy
provides them no useful information in understanding the concept, as
compared to simply providing a simplified-but-correct explanation, and it
leads to the student failing to properly comprehend the more detailed
concepts.
See http://en.wikipedia.org/wiki/Principles_of_learning#Primacy for more
details.
It's well and good to, when introducing the idea of a "value type" to a
student, to limit the information about that idea to the things the
student is capable of comprehending at that moment. I'd argue that value
types aren't really all that complicated and that there's no real need to
limit that information, but every student is different and if there's a
need, fine. But to teach the student WRONG information as a substitute
for teaching them correct information is misguided at best, disrespectful
and irresponsibly deficient at worst.
Pete
Scott said:[...]
Your statement to Greg that the term "structure" to refer to a value type
was incorrect, is itself incorrect.
Point #2:
You stated:
"Any discussion of reference types and value types that begins by mentioning
the heap and the stack is, at a minimum, bound to fall short of precision,
and at worst will be extremely misleading."
The author (who you may have heard of and is considered to be quite an
expert in .NET) explains value-types to the reader in just the same way I
did. In fact, this approach is also taken in "Professional Visual Basic
2008", WROX. And, in fact, it is taken in just about every text I can find
on the subject.
The bottom line for me Peter is that you may know quite a bit about
programming, but you know very little about teaching and despite your edicts
on what teaching technique is "at best" and "at worst" as have been "proven"
as unreliable, every single one of those assertions is absolutely wrong when
it comes to IT Education.
Those who can't teach cite Wikipedia, those who can, do.
My 20 years of experience in IT Education clearly contradicts your several
minutes of Wikipedia searching and conclusions based on no actual experience
in teaching.
Despite what you may personally belive, I (and just a few notable others)
feel that it is appropriate to discuss reference and value types in the
context of the stack and the heap and that it is not necessary to explore
all the gory details during the initial exposure to the concepts.
There's just no argument you can put up to defend youre point, other than
your opinion.
Peter Duniho said:Scott said:[...]
Your statement to Greg that the term "structure" to refer to a value type
was incorrect, is itself incorrect.
Hardly. In the context of C# (which is, of course, the current context),
my statement was quite accurate. Go read the C# specification if you
don't believe me. A VB.NET reference offers no insight whatsoever into
the question.
Yes, I did. A correct statement.
I have not heard of that author, but even if I did, I don't care.
Even if he is an expert in .NET, that doesn't mean he knows the best way
to communicate his knowledge to others, and frankly it doesn't even mean
that he actually has a complete and accurate understanding of value types
in .NET.
Any statement that implies that all value types are stack-allocated is
wrong. And any statement that mentions the stack and value types in the
same sentence without being very clear that value types are allocated on
the stack only in a very specific scenario (i.e. when there's a local
variable having the value type as its declared type), rather than always,
makes just that implication.
And unfortunately, most of the time someone makes that implication, it's
not because they've simply glossed over the question. It's because they
actually _don't know_ how value types work. In fact often, when they are
confronted by the facts of the matter, they will attempt to hide their
prior failure of knowledge by asserting that they were simply "making
things simple" and then entering in a protracted debate as to whether
making false statements in the pursuit of simplicity is an effective
teaching tool or not.
That's quite a brazen claim to make. Quite wrong, of course. But you are
welcome to your uninformed opinion.
Are you suggesting that making reference to well-established facts is not
a useful exercise in a discussion? If so, I am beginning to see why you
are having so much trouble with this.
If the extent of my experience was "several minutes of Wikipedia
searching", there _might_ have been a _smidgen_ of truth to your
accusation.
But that's a false premise, so whatever conclusion comes from it is
useless. How about you try making fewer ASSumptions about what experience
I do and do not have?
And even if I take as granted that you have spent a full 20 years teaching
others in the IT industry, that means very little.
It is much easier to find a bad teacher than a good one. The odds are not
in your favor, if we base your qualifications (or mine) solely on the
length of time spent teaching.
I have never said it was. I simply have stated that one should not make
incorrect claims when teaching a subject.
A teacher has two valid choices when introducing the concept of value
types:
-- Explain that value types are only sometimes allocated on the stack,
and other times allocated on the heap, and explain the differences between
those scenarios
-- Don't mention the stack at all
What a teacher may not do, if they intend to act responsibly, is make a
statement that will mislead a student into thinking value types are always
allocated on the stack.
Frankly, IMHO the second option -- don't mention the stack at all -- is
the correct one. The things that are most important to know about value
types have nothing at all to do with the stack. They have everything to
do with the semantics in the program and how value types are used in the
code.
Later, it is perfectly fine to introduce the more advanced and
implementation-specific details related to value types. After all, that's
part of a complete education. But none of those details are important to
the person just learning about value types.
But, if you feel absolutely compelled to mention the stack when
introducing the concept of value types, you have an obligation to explain
the full monty, and to avoid misleading or incorrect statements.
The key here is: don't teach false facts. They only have to be unlearned
by the student later.
Actually, I provided plenty of references to justify both my comments
about how value types work, as well as what the proper way of introducing
them to students are.
Where are your references? Or do you think is suffices to simply state
your own opinion and expect others to take it as fact?
Scott said:[...]
Well, that says a lot about your ability to be open-minded and see a point
from someone else's point of view. You just don't care. You're right and
no matter what anyone says, you're right. [...]
Peter Duniho said:Scott said:[...]
Well, that says a lot about your ability to be open-minded and see a
point from someone else's point of view. You just don't care. You're
right and no matter what anyone says, you're right. [...]
Just FYI: at this point, the third time in a just a handful of sentences
you've attempted to put words in my mouth, I've stopped reading. You
clearly don't care to carry on any semblance of a mature, factual
discussion.
I won't dispute that some teachers introduce the solar system as an
analogy for atomic structure. However, I will certainly debate the
claim that doing so is actually helpful to the students. There's
practically nothing about the analogy that helps a student
better-comprehend atomic structure.
Scott said:[...]
I'm content to let anyone with enough time on their hands read this thread
and decide who's presented a mature, rational agrument with facts and who
hasn't.
Peter Duniho said:Scott said:[...]
I'm content to let anyone with enough time on their hands read this
thread and decide who's presented a mature, rational agrument with facts
and who hasn't.
Indeed. After all, one person here has made only factual statements and
provided specific Microsoft documentation supporting those statements,
while another person has chosen to defend their position almost entirely
on the basis of mis-characterization of the statements of the other and
use of personal insults.
It should be easy enough for even the casual observer to see which is
which.