简体   繁体   中英

Update Base class values so all derived classes are also updated

I'm hoping to reduce memory requirements on something I'm working for, and ran into a problem with some derived classes. At the moment I have 4 derived classes that inherit the same base class and override the same method, however the functions in the overridden method are different for each derived class. this helped clean up a lot of code and made maintenance a LOT easier. however each derived class has a long[,] File_Vals global variable that becomes incredibly large. here's how the classes are set up.

        public class BaseClass {
            public long[,] File_Vals;
            public int Max_rows;
            
            internal void DoWork(){}
        }

        public class FirstDerivedClass : BaseClass
        {
            public DerivedClass(long[,] file_values, int max_rows)
            {
                File_Vals = file_values;
                Max_rows = max_rows
            }
            
            internal new void DoWork()
            {
                // does work stuff
            }
        }

        public class SecondDerivedClass : BaseClass
        ...

        public class Testing
        {
            FirstDerivedClass firstDerived;
            SecondDerivedClass secondDerived;
            ThirdDerivedClass thirdDerived;
            FourthDerivedClass fourthDerived;
            
            public void Setup(long[,] file_values, int max_rows)
            {
                firstDerived = new FirstDerivedClass(file_values, max_rows);
                secondDerived = new SecondDerivedClass(file_values, max_rows);
                thirdDerived = new ThirdDerivedClass(file_values, max_rows);
                fourthDerived = new FourthDerivedClass(file_values, max_rows);
            }
            ...
            
            public void DoStuff(long[,] file_values, int max_rows)
            {
                // updated versions of file_values and max_rows.
            }
        }

with this there are 4 sets of File_values that have to be updated. is there a way to only have to update the parent/Base class and thus update the other derived/child classes? or is there a way that each derived/child classes don't have their own version of the File_values object and that there's only one of them that they all access? right now the File_values object can have 12 million rows in it, or more, so memory usage is something that needs to be considered.

with this there are 4 sets of File_values that have to be updated

No, in your code in the question there is only one shared instance of long[,] (since arrays are reference types ) and 4 references to it. You can easily check it with next code:

Setup(new long[1,1], 1);
Console.WriteLine(object.ReferenceEquals(firstDerived.File_Vals, secondDerived.File_Vals)); // prints True 

so basically you have 32*4 or 64*4 bits "overhead" depended on platform.

You can make the File_Vals field readonly , to prevent it from being assigned another value (reference), or, better, get-only property.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM