Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Better way of passing properties of base class to derived class?

Tags:

c#

.net-core

Consider having the following code:

public class TheBase
{
   public int One { get; set; }
   public int Two { get; set; }
   public int Three { get; set; }
}

public class Derived : TheBase
{
   public Derived(TheBase theBase)
   {
      One = theBase.One;
      Two = theBase.Two;
      Three = theBase.Three;
   }

   public int Four { get; set; }
   public int Five { get; set; }
}

Is there an easier way to pass the properties "One", "Two" and "Three" from the base class, to the derived class? Is there some kind of a neat hack available, or this is the best optimal solution for such a problem?

like image 280
SpiritBob Avatar asked Nov 01 '25 09:11

SpiritBob


1 Answers

This is the proper implementation:

public class TheBase
{
    public int One { get; set; }
    public int Two { get; set; }
    public int Three { get; set; }
    public TheBase(int one, int two, int three)
    {
        One = one;
        Two = two;
        Three = three;
    }
    public TheBase(TheBase theBase)
    {
        One = theBase.One;
        Two = theBase.Two;
        Three = theBase.Three;
    }
}
public class Derived : TheBase
{
    public int Four { get; set; }
    public int Five { get; set; }
    public Derived(TheBase theBase, int four, int five) : base(theBase)
    {
        Four = four;
        Five = five;
    }
    public Derived(int one, int two, int three, int four, int five) : base(one, two, three)
    {
        Four = four;
        Five = five;
    }
}
like image 111
Marco Salerno Avatar answered Nov 04 '25 00:11

Marco Salerno



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!