Let's say i have a Class called Field
public class Field
{
public string name {get;set;}
public string type {get;set;}
}
For my program, i would need to build a List, but some Field in the List are general and would duplicate in few part of program, I would like to separate those Field that will be duplicated into a base class so that those sub classes can inherit from the base class without having some Field duplicated. The concept is roughly as below:
base classA {
List<Field> list = new List<Field>();
list.add(new Field(){name = "fieldNameA", type = "typeA"});
list.add(new Field(){name = "fieldNameB", type = "typeB"});
list.add(new Field(){name = "fieldNameC", type = "typeC"});
}
base classB {
List<Field> list = new List<Field>();
list.add(new Field(){name = "fieldNameX", type = "typeX"});
list.add(new Field(){name = "fieldNameY", type = "typeY"});
list.add(new Field(){name = "fieldNameZ", type = "typeZ"});
}
sub class {
private void methodA() {
//Inherits list initialized at
// **base classA** above,
//and continues to initialize some other Fields
list.add(new Field(){name = "fieldNameD", type = "typeD"});
list.add(new Field(){name = "fieldNameE", type = "typeE"});
//so at here finally i would have list which consists of fieldNameA to fieldName E
}
private void methodB() {
//Inherits list initialized at
// **base classB** above,
//and continues to initialize some other Fields
list.add(new Field(){name = "fieldNameD", type = "typeD"});
list.add(new Field(){name = "fieldNameE", type = "typeE"});
//so at here finally i would have list which consists of fieldNameX, Y,Z,D,E
}
}
How am i supposed to do in order to achieve this?