Jack M Jack M - 2 months ago 19
C# Question

base class implementation in C#

I am trying to implement a model so that the base class has like 5 properties and derived class will be implementing what it needs. as an example:

public class BaseClass
{
public int a{get;set;}
public int b{get;set;}
public int c{get;set;}
public int d{get;set;}
public int e{get;set;}
}


and on the derived classes i want to have something like this:

public class DerivedClass: BaseClass
{
public override int a{get;set;}
public int derivedClass1 {get;set;}
}

public class DerivedClass1 :BaseClass
{
public override int a{get;set;}
public override int b{get;set;}
public int derivedClass1 {get;set;}
}

public class DerivedClass2 :BaseClass
{
public override int a{get;set;}
public override int b{get;set;}
public override int c{get;set;}
public int derivedClass2 {get;set;}
}

public class DerivedClass3:BaseClass
{
public override int a{get;set;}
public override int b{get;set;}
public override int c{get;set;}
public override int c{get;set;}
public int derivedClass3 {get;set;}
}


by doing so I need to implement all base class objects. Is there a way of partially implementing the derived class like this

Thanks

Uno Uno
Answer

If I understood the question, you would like to optionally rewrite the implementation of the properties in the base class.

You can use the virtual keyword:

public abstract class BaseClass
{
    public virtual int a { get; set; }
    public virtual int b { get; set; }
    public virtual int c { get; set; }
    public virtual int d { get; set; }
    public virtual int e { get; set; }
}

in such a way you can optionally override it in the derived classes:

public class DerivedClass: BaseClass
{
    public override int a { get; set; }
    public int derivedClass1 { get; set; }
}