Generic Type Parameters parameter interface c#

I liked this concept because it seems kind of sneaky. By that I mean, in the past I was only able to call a method from a base class via inheritance or creating an instance of the class itself. However, now by implementing a generic type parameter we can achieve the same thing.

First we create a standard interface, just like we have always done. It identifies a method called WriteCSharp and a property named BaseMessage. Both of which must be implemented in a class which implements this interface. They must have an access modifier of public too. Next we implement the generic type interface IProgramExtended which takes T as a parameter. The IProgramExtended interfaces implements the IProgram interface,

//Standard Interface
  public interface IProgram
  {
      void WriteCSharp();
      string BaseMessage { get; set; }
  }
 
  //Generic Interface which implements the standard interface
  public interface IProgramExtended<T> where T : IProgram
  {
      void Run(T t);
      string ExtendedMessage { get; set; }
  }

We write a class that implements the IProgram interface. Note that is does implement the required method and property as public.

//Class which implements standard interface
public class MyProgram : IProgram
{
     public void WriteCSharp()
     {
         Console.WriteLine(BaseMessage);
         Console.ReadLine();
     }
 
     public string BaseMessage { get; set; }
}

We implement the above class with the below lines of code.

MyProgram mp = new MyProgram();
mp.BaseMessage = "Message from MyProgram";
mp.WriteCSharp();

Now the fun part. We create another class which implements the IProgramExtended interface and passes the MyProgram class we created above. We implement the required method and property of the Extended interface.

//Class which implements generic interface with 
//class that implements standard interface
public class MyExtendedProgram : IProgramExtended<MyProgram>
{
     public void Run(MyProgram t)
     {
         Console.WriteLine();
         Console.WriteLine(ExtendedMessage);
         Console.WriteLine();
         t.BaseMessage =
          "Message from MyProgram changed by MyExtendedProgram";
         t.WriteCSharp();
     }
 
     public string ExtendedMessage { get; set; }
}

When we implement the above class, like the below we receive these results. We call the method in our extended class, the one which implemented the generic typed interfaces to and call its Run method. Within the run method we print out our message first. The magic here is that we can access the property and the method of a class we did not directly inherited from or instantiate. Like I said, I find that cool and sneaky…

image

MyExtendedProgram mp2 = new MyExtendedProgram();
mp2.ExtendedMessage = "Message from MyExtendedProgram";
mp2.Run(mp);

I stumbled on this while I was trying to find a way to return a different type from a method in a derived class. Generics are a very cool invention.

Download the source