A
alexey.sviridov
whay all types in .net are inherited from System.Object ?
I am quoting following lines from .net Components Book from Juval Lowy
Page:
544
"The abstract class Type, defined in the System namespace, is an
abstraction
of a .NET CLR type. Every .NET type, be it a .NET-provided type(from value
types such as integers and enums to classes and interfaces) or a
developer-defined type, has a corresponding unique Type value.
The canonical base class of any .NET type is System.Object. ..."
If I am understanding it right, you mentioned in your second posting, "All
types are not. All classes are." to the original question "whay all types
in
.net are inherited from System.Object ?"
int a;
a = 10; //literal or naked integer
int b = a.GetHashCode(); // Will 'a' be Boxed or remain naked integer ?
string c = a.ToString(); // Will 'a' be Boxed or remain naked integer ?
object o = (object) i; //Will be boxed.
Kevin Spencer said:You're quite welcome. This stuff can be confusing.
As long as you treat the variable "a" as if it were an integer, it will
remain unboxed, as in these 2 cases.
In these cases, you are calling a method of the System.Int32 class, in which
case the variable is boxed as an instance of the System.Int32 class.
Kevin Spencer said:I certainly did step on the class/struct thing. In too much of a hurry, I'm
afraid. I did know that Int32 is a struct, but got sloppy. Been putting in
too many hours lately.
As for the boxing explanation, I did not realize that. I was certain that
GetHashCode or ToString would result in a boxing operation, because they are
both methods of System.Object, and they are listed in the SDK as being
overridden. The assumption I made was that they override the System.Object
methods, rather than the System.ValueType methods.
Thanks for the ...clarification?
Kevin Spencer said:Well, Jon, keep on sharing what you've learned! Aside from the copious and
varied research I must do on a daily basis in order to do my job, my time in
these newsgroups tends to fill in some of the remaining gaps in my
understanding, one way or another, and I always enjoy your insights into the
lower levels of the .Net technology, which I often don't have enough time to
research as thoroughly as I would like to otherwise (unfortunately, I can
only go without so much sleep!). When I participate here, I can contribute
what I know, as well as learning something new from time to time, which are
both worth my time.
(Another thing to do to find nooks and crannies is to read the specs.
I've recently been reading the C# 2.0 draft ECMA specs from start to
end, and there are all kinds of things I never knew about. Have you
ever seen people overloading the true and false operators, for
instance?)
So to conclude this discussion on boxing of 'Int', here are the
conclusions that I am deriving form our dicussion. Please correct me if
I am wrong.
1. Calling GetHashCode(), GetTypeCode or ToString() on an 'int' will
NOT cause it to be boxed.
Correct.
2. Calling GetType() on int variable or specifically casting an 'int'
to an object will cause it to be boxed.
Type t = i.GetType(); //Box
object o = (object) i; //Box
3. In case of non-boxed operation on an 'int'( which being struct is
nothing but a sealed class),
compiler basically calls
System.Int32:corresponding operation) e.g.
System.Int32::GetHashCode().
Regarding int being "Well, a sealed type - it's not a class. ":
When I look at IL of mscorlib.dll for Int32, the first line it shows
is:
Int32
.class value public sequential ansi serializable sealed
beforefieldinit
Although i understand that saying it a type is not wrong becuase
integer, enum, classes, interfaces etc in .net terms are all types but
do you think being specific about struct and saying it a sealed class
is wrong?
Please correct me if I am wrong in saying that System.Int32 (struct) is
a sealed class which inherits from System.ValueType class and which
inherits from System.Object class.