I understand how to go between the 2 different types but my question is; why is this difference at the type level?
I would have thought it would be a property of the ParamterInfo object, not separate special type.
Assuming it’s presented as a separate type in reflection because that is how it is internally, what language benefits are there to having this as a separate type (I’m guessing easier method overload resolution or something)?
On a similar note, why does ref make a distinct type but out doesn’t (I can’t think of reasons for ref being a separate type, that don’t apply to out)?
The assignment to
localmeans that the place in memory thatlocallabels now points to the same object thatSomeStaticlyHeldStringwas pointing to.The assignment to
strParammeans that the place in memory that the argument passed to the method usingreflabels, now points to the same object thatSomeStaticlyHeldStringwas pointing to.Obtaining
local.Lengthqueries the object thatlocalpoints to. ObtainingstrParam.Lengthqueries the object that the variable thatstrParampoints to, points to.The two are really behaving very differently, not just at the point the parameter or local is defined, but with every use of them. That the difference is largely hidden makes them all the more different, as every operation upon them differs in effect.
If we had a lower level language that had nothing but local variables, objects on some sort of non-local heap, and pointers to both of those, then
localwould be of typestring*andstrParamof typestring**. This would be how we would do a similar sort of operation in C, and how we could do so in C++ though it also has reference types (though with C++ that a type is a reference type is more clearly part of its type definition, and they have further uses and refinements). C# hides almost all of this from us in its syntax. It’s always debatable how beneficial any hiding of details is, but in this case there isn’t much in the way of anything useful being hidden, so it’s one that one would be hard-pressed to criticise.