Search code examples
c#inheritanceinterfacemultiple-inheritance

Using base class and base interface in C#


I am reshaping an entire system that does not use base classes and base interfaces.

My idea to do so is to extract all the common methods to a base classes and base interfaces.
So basically, we would have:

  • A base class SomeClassBase implementing an interface ISomeClassBase

  • A derived class SomeClassDerived implementing ISomeClassDerived (this interface deriving from ISomeClassBase)

Now the problem, how can I instantiate "_mySession" in the derived class (which has a different cast than in the base class), while preserving all the methods from the base class:

public class SomeClassBase : ISomeClassBase 
{
 public IMySessionBase _mySession = MySession.Instance();

 public SomeClassBase ()
 {
   _mySession.connect();  // Needed??
 }

 public void doSomething()
 {
  _mySession.doSomething();
 }
}

public class SomeClassDerived : SomeClassBase, ISomeClassDerived  
{
 public IMySessionDerived _mySession = MySession.Instance();

 public SomeClassDerived ()
 {
  _mySession.connect();
 }

 public void doSomethingElse()
 {    
  _mySession.doSomethingElse();
 }
}

One more thing, IMySessionDerived implements IMySessionBase.


Solution

  • Pasting @Selvin answer instead of the link buried in the comments:
    The trick here is to use the keyword "base()"

    using System;
    using System.Runtime.CompilerServices;
    
    public class Program
    {
        public static void Main()
        {
            var o1 = new O1();
            o1.DS1();
            var o2 = new O2();
            o2.DS1();
            o2.DS2();
        }
    
        public class Session1
        {
            protected readonly Type ownerType;
            public Session1(Type type)
            {
                ownerType = type;
            }
    
            public virtual void DS1([CallerMemberName] string functionName = "")
            {
                Console.WriteLine(ownerType.Name + ":" +  GetType().Name + ":" + functionName);
            }       
        }
    
        public class Session2 : Session1
        {
            public Session2(Type type):base(type) { }
    
            public virtual void DS2([CallerMemberName] string functionName = "")
            {
                Console.WriteLine(ownerType.Name + ":" +  GetType().Name + ":" + functionName);
            }   
        }
    
        public class O1
        {
            private readonly Session1 t;
    
            public O1() : this(new Session1(typeof(O1))) { }
            protected O1(Session1 t)
            {
                this.t = t;
            }
            public void DS1()
            {
                t.DS1();
            }
        }
    
        public class O2 : O1
        {
            private readonly Session2 t;
    
            public O2() : this(new Session2(typeof(O2))) { }
            protected O2(Session2 t) : base(t)
            {
                this.t = t;
            }
    
            public void DS2()
            {
                t.DS2();
            }
        }
    }