title: Can a Base Class Method return the type of the derived class?
body: <p>Based on the other posts I have read, it seems that this may not be possible, but I thought I would post what I am trying to do and see if anyone knows of a solution. </p>
  
  <p>I am trying to add a "Clone()" method to classes generated from a Telerik Open Access domain model. No problem. I was able to figure out how to add a base class to the generated entity models so that I could identify those classes by their base class. (<a href="http://www.telerik.com/forums/all-entities-inherit-from-a-base-class" rel="nofollow noreferrer">All entities inherit from a base class</a>)</p>
  
  <p>I want ALL these entity model classes to be able to Clone themselves. I have found a solution for that as well. (<a href="https://stackoverflow.com/questions/78536/deep-cloning-objects" title="Deep Cloning Objects">Deep Cloning Objects</a>)</p>
  
  <p>Now I have a base class and I want to add a Clone() function to every class that derives from that base class. So ... it seems that the base class is the natural place to put it ... right?  </p>
  
  <pre><code>public abstract class DBEntityBase
  {
      /// &lt;summary&gt;
      ///     Gets a COPY of the instance in it's current state
      /// &lt;/summary&gt;
      /// &lt;typeparam name="T"&gt;&lt;/typeparam&gt;
      /// &lt;returns&gt;&lt;/returns&gt;
      protected T Clone&lt;T&gt;()
      {
          return JsonConvert.DeserializeObject&lt;T&gt;(JsonConvert.SerializeObject(this));
      }
  }
  </code></pre>
  
  <p>I add the <em>protected generic Clone()</em> method because at the base class level we don't know the Type we are cloning. The Clone() method needs to be implemented by the entity model itself in order to provide the specific type being cloned. </p>
  
  <pre><code>public partial class DeliverableEntity
  {
      public new DeliverableEntity Clone()
      {
          return this.Clone&lt;DeliverableEntity&gt;();
      }
  }
  </code></pre>
  
  <p>This works fine, but does not <strong><em>guarantee</em></strong> that derived classes will publicly expose a Clone() method, so I added an <em>abstract Clone()</em> method to the base which will <strong><em>require</em></strong> the derived class to implement a public Clone() method.</p>
  
  <pre><code>public abstract class DarkRoomDBEntityBase
  {
      /// &lt;summary&gt;
      ///     Gets a COPY of the instance in it's current state
      /// &lt;/summary&gt;
      /// &lt;typeparam name="T"&gt;&lt;/typeparam&gt;
      /// &lt;returns&gt;&lt;/returns&gt;
      protected T Clone&lt;T&gt;()
      {
          return JsonConvert.DeserializeObject&lt;T&gt;(JsonConvert.SerializeObject(this));
      }
  
      /// &lt;summary&gt;
      ///     Gets a deep COPY of the entity instance
      /// &lt;/summary&gt;
      /// &lt;returns&gt;&lt;/returns&gt;
      public abstract DBEntityBase Clone();
  }
  </code></pre>
  
  <p>I give it a return type of the base class itself; every implementation MUST return a value which adheres to the DBEntityBase, which of course, all derived classes do.  Since the Clone() method is returning a type of the derived class itself then ... it seems to make sense that this would work.</p>
  
  <pre><code>DeliverableEntity originalEntity = new DeliverableEntity();
  DeliverableEntity clonedEntity   = originalEntity.Clone();
  </code></pre>
  
  <p>When building, however, I get the error ..</p>
  
  <blockquote>
    <p>'DeliverableEntity' does not implement inherited abstract member
    'DBEntityBase.Clone()'</p>
  </blockquote>
  
  <p>Presumably due to the return type. </p>
  
  <p>I know that I COULD just put the Clone() method in a separate utility class and not implement it directly in each entity model ... that would get me around my problem (and probably save a lot of implementation code), but I am still left wondering why this won't work. It seems like there should be a way to do this.</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>In response to @Luann's first reply (thank you) I made the change to "override" ...</p>
  
  <pre><code>public partial class DeliverableEntity
  {
      public override DeliverableEntity Clone()
      {
          return this.Clone&lt;DeliverableEntity&gt;();
      }
  }
  </code></pre>
  
  <p>and am now receiving the following error ... </p>
  
  <blockquote>
    <p>return type must be 'DBEntityBase' to match overridden member
    'DBEntityBase.Clone()'</p>
  </blockquote>
  
  <p><strong>SOLUTION</strong> </p>
  
  <p>Thanks to Flynn1179 I was able to move forward again. I thought I would take a moment to document what I did here for future reference ..</p>
  
  <p>Instead of creating a partial class for each entity model in the ORM, implementing an abstract method, I created a single extension method as suggested.</p>
  
  <pre><code>namespace DAL
  {
      public partial class DeliverableEntity : DBEntityBase
      {
           // ... Code generated from ORM 
      }
  
      public partial class DeliverableItemEntity : DBEntityBase
      {
           // ... Code generated from ORM 
      }
  
      public partial class DeliverableItemAttrEntity : DBEntityBase
      {
           // ... Code generated from ORM 
      }
  }
  
  namespace DAL
  {
      public static class EntityExtensionMethods
      {
          public static T Clone&lt;T&gt;(this T entity) where T: DBEntityBase
          {
              return JsonConvert.DeserializeObject&lt;T&gt;(JsonConvert.SerializeObject(entity));
          }
      }
  }
  </code></pre>
  
  <p>Some things to note ... </p>
  
  <ul>
  <li>Put this class in the same Namespace as the entity models. If it's in a different namespace then you will need to add that namespace in order to have access to the method.</li>
  <li>I defined the constraint on the generic type to only and all classes which inherit the <em>DBEntityBase</em> class. Since I made all my entity model classes derive from this base class I know that all of them will will expose this method but also that any class which does NOT derive from this class will NOT have this capability.</li>
  <li>The extension method and the class that contains it must be <em>static</em></li>
  </ul>
  
  <p>Now the cool part is that ALL the Entities have access to the function ...</p>
  
  <pre><code>    // Create the original instances of the entities    
      DeliverableEntity origDeliverable       = new DeliverableEntity();
      DeliverableItemEntity origItem          = new DeliverableItemEntity();
      DeliverableItemAttrEntity origItemAttr  = new DeliverableItemAttrEntity();
  
      // now here's the magic 
  
      DeliverableEntity cloneDeliverable      = origDeliverable.Clone();
      DeliverableItemEntity cloneItem         = origItem.Clone();
      DeliverableItemAttrEntity cloneItemAttr = origItemAttr.Clone();
  </code></pre>
  
  <p>I love this solution as it's has the simplicity of a base class where the implementation is defined in a single location (whereas I was looking at implementing an abstract method individually in each derived class) plus since it's associated with the DBEntityBase class and in the same namespace, it becomes part of the "contract' defined by the base class which means I can count on it being available whenever I have a class derived from DBEntityBase.</p>