class Base
{
//...
public int i = 5;
}
class Drifted : Base
{
//...
public int b = 10;
}
Base ObjectOrReference = new Drifted();
So Base ObjectOrReference;is reference to the base type.
When we write Base ObjectOrReference = new Drifted(); it becomes object because by using ‘new’ we allocate memory for it? Or it still reference and if it is true, what type does it have?
Direct question is: “Is ObjectOrReference object?”
It’s still a reference, a pointer to the object on the heap. It is of type
Drifted.Even though your reference is of the
Basetype, the underlying object isDriftedand any overridden members in the derived classDriftedwill be used instead of those onBase, even though you are trying to useBase.In the case of member hiding using the
newsyntax, if you have a reference to the base type it will bypass any derived classes that are hiding the members.An overview can be found online with Googling for “C# Reference Types”. I skimmed this, looks like a worthwhile read:
http://www.albahari.com/valuevsreftypes.aspx