0

I believe this question is fairly basic but I am having trouble finding an answer to this question. In C# let's say I have 3 classes: A, B, C

B derives from A

C derives from B

Now, if I wanted a list or array of objects of type class A but wanted the array to be able to house objects of type B and C this is no problem... I could do something like this:

A[] myArrayofAtypes;

However let's say I make the first element of this array of type C. If type C has a variable defined in its class definition that ONLY exists in class C's class definition... how do I access that variable from the array? I can't just do A[0].MyVariableGetter as that variable does not exist in the base class, A.

Any suggestions?

4
  • creating an array of A[] and holding everything there isn't polymophism
    – ro-E
    Commented Mar 15, 2015 at 17:17
  • My question had factors pertaining to polymorphism so I figured marking that as a category for the question was appropriate. Also, I never said creating an array of A[] and holding everything there was polymophism.
    – Brad Mash
    Commented Mar 15, 2015 at 17:52
  • your question is about casting and inheritance.. once you check the object's type you aren't polymorphic
    – ro-E
    Commented Mar 15, 2015 at 19:20
  • Would it make you feel better if I said inclusion polymorphism?
    – Brad Mash
    Commented Mar 15, 2015 at 19:28

3 Answers 3

1

You have to downcast it:

C c = (C)myArrayofAtypes[0];
var v = c.MyVariableGetter;
9
  • Is downcasting processing intensive if it is done a lot? I mean, imagine an array of 100 elements and every element needs downcasted all on succession to access their details... Is this a bad thing for processing time / resources?
    – Brad Mash
    Commented Mar 15, 2015 at 17:17
  • 100 elements? nope. More than few ten thousands? Possibly.
    – mcy
    Commented Mar 15, 2015 at 17:19
  • @BradMash check this.
    – dario
    Commented Mar 15, 2015 at 17:19
  • If I implement what you did king.code then to actually update the array element so it gets the updated object at position [0] in the array do I need to then do myArrayofAtypes[0] = c ? Or do I have to upcast again and then do myArrayofAtypes[0] = c ?
    – Brad Mash
    Commented Mar 15, 2015 at 17:31
  • @BradMash since your array holds A references you have to always downcast. I mean, if you do this myArrayofAtypes[0] = (C)myArrayofAtypes[0];, you are downcasting and then upcasting again.
    – dario
    Commented Mar 15, 2015 at 17:34
0

You have to type cast it to type c and access the member.

If (arr[i] is c)
{
    ((C)arr[0]).member = value;
 }
0

Instead of casting as 'king.code' has suggested like this I would use these pretty C# operators which are 'is' and 'as'.

public void CallIfTypeC(A a)
{
    if (a is C)
    {
        C c = a as C;
        c.DoAction();
    }
}
1
  • That makes sense but let me ask you this... can I do the downcasting, update A[0] in my example (update this instance's class C specific variables) and have that instance updated in the array? Does that make sense?
    – Brad Mash
    Commented Mar 15, 2015 at 17:21

Not the answer you're looking for? Browse other questions tagged or ask your own question.