title: Fluent interfaces and inheritance in C#
body: <p>I'll show a problem by example. There is a base class with fluent interface:</p>
  
  <pre><code>class FluentPerson
  {
      private string _FirstName = String.Empty;
      private string _LastName = String.Empty;
  
      public FluentPerson WithFirstName(string firstName)
      {
          _FirstName = firstName;
          return this;
      }
  
      public FluentPerson WithLastName(string lastName)
      {
          _LastName = lastName;
          return this;
      }
  
      public override string ToString()
      {
          return String.Format("First name: {0} last name: {1}", _FirstName, _LastName);
      }
  }
  </code></pre>
  
  <p>and a child class:</p>
  
  <pre><code>class FluentCustomer : FluentPerson
  {
      private long _Id;
  
      private string _AccountNumber = String.Empty;
  
      public FluentCustomer WithAccountNumber(string accountNumber)
      {
          _AccountNumber = accountNumber;
          return this;
      }
      public FluentCustomer WithId(long id)
      {
          _Id = id;
          return this;
      }
  
      public override string ToString()
      {
          return base.ToString() + String.Format(" account number: {0} id: {1}", _AccountNumber, _Id);
      }
  }
  </code></pre>
  
  <p>The problem is that when you call <code>customer.WithAccountNumber("000").WithFirstName("John").WithLastName("Smith")</code> you can't add <code>.WithId(123)</code> in the end because return type of the <code>WithLastName()</code> method is FluentPerson (not FluentCustomer).</p>
  
  <p>How this problem usually solved?</p>