title: How do you write a C# Extension Method for a Generically Typed Class
body: <p>This should hopefully be a simple one.</p>
  
  <p>I would like to add an extension method to the System.Web.Mvc.ViewPage&lt; T > class.</p>
  
  <p>How should this extension method look?</p>
  
  <p>My first intuitive thought is something like this:</p>
  
  <pre><code>namespace System.Web.Mvc
  {
      public static class ViewPageExtensions
      {
          public static string GetDefaultPageTitle(this ViewPage&lt;Type&gt; v)
          {
              return "";
          }
      }
  }
  </code></pre>
  
  <p><strong>Solution</strong></p>
  
  <p>The general solution is <a href="https://stackoverflow.com/questions/68750/how-do-you-write-a-c-extension-method-for-a-generically-typed-class#68772">this answer</a>.</p>
  
  <p>The specific solution to extending the System.Web.Mvc.ViewPage class is <a href="https://stackoverflow.com/questions/68750/how-do-you-write-a-c-extension-method-for-a-generically-typed-class#68802">my answer</a> below, which started from the <a href="https://stackoverflow.com/questions/68750/how-do-you-write-a-c-extension-method-for-a-generically-typed-class#68772">general solution</a>.</p>
  
  <p>The difference is in the specific case you need both a generically typed method declaration AND a statement to enforce the generic type as a reference type.</p>