Jeff Louie said:
Jon... If this means M(E) I wonder if this is just saying that if M is
declared in a reference type and is passed E that is a value type,
then E will be boxed.
No, it's not M(E). If you look at the bit of the spec I was referring
to, E is the instance expression - i.e. what the method is going to be
invoked *on*.
So System.Console.WriteLine(valueType) valueType is boxed whereas
valueType.ToString() there is no boxing. Either way all valueTypes honor
the ToString() method which appears to represent polymorphic behavior.
No, the value types themselves don't honour ToString - their boxed
equivalents do, because the boxed types derive (indirectly) from
Object.
The difference here is that C# contrives to make it look like value
types derive from Object indirectly, but the CLR doesn't - and IMO it's
important to know the difference. Indeed, the C# spec actually refers
to value types deriving from Object implicitly, which I think is part
of the problem - it's a bad way of specifying the language, as it hides
the nature of value types as far as the CLR is concerned. I wouldn't be
surprised if analysing the C# spec closely would throw up some
inconsistencies due to this.
Take, for example, section 8.2.4 (which is part of section 8 which is
only a note, admittedly) - it states: "An int value can be converted to
object and back again to int". If int actually derives from object,
there shouldn't be any conversion necessary, just as it would make no
sense to talk about converting a string to an object.
Put it this way - while the CLR explanation (with its boxed type of the
same name business) is slightly more complicated at first sight, it's a
lot simpler in the long run. You don't need to treat inheritance with
value types as different from inheritance with reference types, because
you just state that there *is* no inheritance with value types.
It's a little bit like my old bugbear of people saying that objects are
passed by reference - it's supposedly a simplification, but the hoops
you have to jump through just to avoid stating the truth aren't worth
it IMO.