4
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?"

Nikita
  • 315
  • 1
  • 7
  • 14
  • Could you please rephrase your question? – FIre Panda May 17 '11 at 13:28
  • Basically I was asked Does 'ObjectOrReference' reference OR object ? As I understand it simultaneously reference and and limited object of 'Drifted' class by limited I mean it has access only to members defined in 'Base'. – Nikita May 17 '11 at 13:50

5 Answers5

12

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 Base type, the underlying object is Drifted and any overridden members in the derived class Drifted will be used instead of those on Base, even though you are trying to use Base.

In the case of member hiding using the new syntax, 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

Adam Houldsworth
  • 63,413
  • 11
  • 150
  • 187
  • Why I can assign value to the 'ObjectOrReference.i' And what is the name that object in heap if 'ObjectOrReferenc' is not object. – Nikita May 17 '11 at 13:29
  • @Nikita because that is a member of the `Base` type. Using the `ObjectOrReference` local variable you cannot see `Drifted.b`, you would need to cast to `Drifted` to see that. – Adam Houldsworth May 17 '11 at 13:32
  • 1
    @Nikita C# hides the fact that it's actually pointing to a memory location on the heap. C# references are automatically resolved to the object for you, whereas in C++ you would need to resolve the pointer yourself. – Adam Houldsworth May 17 '11 at 13:34
7
Base ObjectOrReference = new Drifted();

with that, you have created a Drifted instance but is referenced by Base reference variable ObjectOrReference. Right now it's capabilities are limited to that of what's available in Base. If you want to, say access int b in Drifted, you'll have to cast it to Drifted

Bala R
  • 107,317
  • 23
  • 199
  • 210
7

Yes, ObjectOrReference is an object and a valid object.

It is an object of Drifted Type. It is a basic concept of Object Oriented Programming that pointer/ref of base class(ObjectOrReference in your case) can hold/refer to object of it's derived classes(Drifted in your case) thats why it is not an error and a valid object of Drifted type

Haris Hasan
  • 29,856
  • 10
  • 92
  • 122
6

It is and always be a reference. Think of object variables as pointers/refernces. While the actual object creation/allocation happens on heap, references are created on local stack space.

d-live
  • 7,926
  • 3
  • 22
  • 16
1

You have instantiated the object of a derived class but are refering to its memory location through its base class reference type (ObjectOrReference). Object reference knows the member of its type only (i.e with in its base class) and is completely unaware about the member of its derived class. So you can't access the member of an derived class and you can't type cast here as we are talking about inheritance

Jake1164
  • 12,291
  • 6
  • 47
  • 64
Suresh
  • 518
  • 6
  • 18