I'm just learning to use C# Inheritance.
It is quite clear to me (I've worked with C++ in the past).
I've a problem with two classes.
So, imagine a base class, which contain a sub-class; the sub-class is used to collect data.
I used a sub-class because I need to write and read it from a disk, using a XmlSerializer.
The base class has the needed public Save/Load members, that serialize the sub-class.
I now have to Inherit the base class multiple times; the only differnece in the derived classes is the sub-class, that have a slightly different format for each derived class.
I created few derived classes, re-definind into them the sub-class wth the keyboard "new".
Unfortunately, everytime I call the Save/Load members (defined in the base class) from the derived classes, these methods actually save ansd load the sub-class defined into the base class, not the one defined into the calling derived class.
What I'm doing wrong?
Here below an excerpt of my code, with the definition of the two classes (heavily simplified, of course):
public class BaseClass
public class Data
public int data1 = 1;
public int data2 = 1;
public string Data3 = "BaseString";
public Data data = new Data();
public bool Save(string Filename)
var xs = new XmlSerializer(Data.GetType());
using (TextWriter sw = new StreamWriter(Filename))
public static T Load<T>(Filename)
var xs = new XmlSerializer(typeof(T));
using (var sr = new StreamReader(FileName))
rslt = (T)xs.Deserialize(sr);
public DerivedClass : BaseClass
public new class Data
public int data1 = 2;
public int data2 = 2;
public string Data3 = "DerivedString";
public int Data4 = 2;
public new Data data = new Data();
Now, using them as follows gives me the error that the base class Data are saved:
DerivedClass MyClass = new DerivedClass();
I know this is probably quite trivial, but please help me.
Thank you in advance.