0

I have an array of objects, and I want to use a setter function to update one of them. However, the array's type is of the parent class, which doesn't have the setter function. So I type casted the array to the correct class, like this example:

class Parent {
}

class Child : Parent {
    private int n = 0;

    //function specific to Child
    public void updateN(int number)
    {
        n = number
    }
}

class Main {
    static void main(string[] args)
    {
        Parent[] a = new Parent[];
        a[0] = new Child();
        Child c = (Child)a[0];
        c.updateN(1);
    }
}

Now, does the code at the bottom update the Child object on the array? Or does it only update the object stored in variable c. If not, whats the best way to update the object in the array?

root
  • 3
  • 1
  • 4
  • `c` will still point to the same object – Fabio Jan 05 '21 at 00:16
  • 2
    You can verify Fabio's comment with `object.ReferenceEquals(a[0], c)`, which will result in true. – David L Jan 05 '21 at 00:19
  • Note that this will throw an exception if, for some reason, `a[0]` is not a `Child`. Consider using `Child c = a[0] as Child;`. If `a[0]` is not a `Child`, `c` will be null. Then you could handle the null appropriately (perhaps something like `c?.updateN(1);` (which will only call `updateN` if `c` is non-null) – Flydog57 Jan 05 '21 at 00:28
  • Classes are reference types; casting a reference only changes the compiler's _perception_ of the reference, not the reference itself. It's the same object, however you look at it. See duplicate. – Peter Duniho Jan 05 '21 at 07:50

2 Answers2

0

There was some error in you code but I think, I have understand, what you want to do. I have provided a solution code below. Please check and try it and let me know it works or not for you.

class Parent
{
}

class Child : Parent
{
    private int n = 0;

    //function specific to Child
    public void updateN(int number)
    {
        n = number;
    }
}
class ChildofChild : Parent
{
    private int n = 0;

    //function specific to Child
}
class Main
{
    static void main(string[] args)
    {
        Parent[] a = new Parent[10];
        a[0] = new Child();
        a[1] = new ChildofChild();
        if (a[0] is Child) //a[0] is a Child so it will enter in this "if" segment.
        {
            Child c = (Child)a[0];
            c.updateN(10);
        }
        if (a[1] is Child) //a[1] is Not a Child so it will not enter in this "if" segment.
        {
            Child c = (Child)a[1];
            c.updateN(10);
        }

    }
}

Inside the Main method, I have created two different type of object for Parent array. To make code little safer, I have used " if (a[1] is Child) " condition. From my point of view, if you know which class contains "updateN" method then you can add that class to that "if" condition segment and can update your desire value using proper casting.

Srijon Chakraborty
  • 2,007
  • 2
  • 7
  • 20
0

Short answer: yes, the object in the array will be updated

Details:

Class variables are actually pointers in C#. So in your code pointer from the array is copied then "updateN" is called. The actual object was not copied, so the method is called on the same object in the array.

Andrii Shvydkyi
  • 2,236
  • 1
  • 14
  • 19