Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4575464
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T20:04:02+00:00 2026-05-21T20:04:02+00:00

I have some classes that i’d like chain methods of to provide a fluent

  • 0

I have some classes that i’d like chain methods of to provide a fluent style of config.

e.g.

public class BaseFoo
{
    private bool _myProp = false;
    public [[[BaseFoo?]]] SetMyProperty()
    {
        this._myProp = true;
        return this;
    }
}

public class DerivedFoo : BaseFoo
{
    public bool _derivedProp = false;
    public DerivedFoo SetDerivedPropery()
    {
        this._derivedProp = true;
        return this;
    }
}

The problem is clearly when trying to chain these together, as soon as I use the base method the returned type of BaseFoo. Obviously I could cast this back to DerivedFoo, but is there a simply way of returning an object of the derived class type.

The only way I can think of doing it is chaining constructors together and passing in the parent type to the initial constructor but usure of syntax.

Another way would be to have like proxy methods for each of the subclass’, but returning the derived type.

 DerivedFoo foo = new DerivedFoo();
        foo.SetMyProperty().SetDerivedPropery(); // wont work because the SetMyProperty returns type of BaseFoo
        foo.SetDerivedPropery().SetMyProperty(); // works because I know i'm calling the method of the derived class first
        (foo.SetMyProperty() as DerivedFoo).SetDerivedPropery(); // works as i'm casting result of base method to derived type

any ideas?

Thanks in advance,

Sam

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T20:04:03+00:00Added an answer on May 21, 2026 at 8:04 pm

    What about generics?

    public class BaseFoo<TDerived> where TDerived : BaseFoo<TDerived>
    {
        private bool _myProp = false;
        public TDerived SetMyProperty()
        {
            this._myProp = true;
            return (TDerived)this;
        }
    }
    
    public class DerivedFoo : BaseFoo<DerivedFoo>
    {
        public bool _derivedProp = false;
        public DerivedFoo SetDerivedPropery()
        {
            this._derivedProp = true;
            return this;
        }
    }
    

    But I really think that the coupling between the different levels of the inheritance hierarchy is a big design problem.
    I suggest you have a look at other fluent interfaces like Fluent NHibernate, Rhino Mocks etc, to see how the “big” guys do it 😉

    You could also use extension methods like this, sort of an fluent upgrade. This seems cleaner to me.

    public static class FooFluentExtensions
    {
        public static T SetMyProperty<T>(this T foo) where T : BaseFoo
        {
            foo.MyProp = true;
            return foo;
        }
    
        public static T SetDerivedPropery<T>(this T foo) where T : DerivedFoo
        {
            foo.DerivedProp = true;
            return foo;
        }
    }
    
    static void Main()
    {
        DerivedFoo foo = new DerivedFoo();
        // works, because SetMyProperty returns DerivedFoo
        foo.SetMyProperty().SetDerivedPropery();
    
        BaseFoo baseFoo = new BaseFoo();
        baseFoo.SetMyProperty();
    
        // doesn't work (and that's correct), because SetMyProperty returns BaseFoo
        // baseFoo.SetMyProperty().SetDerivedPropery();
    }
    

    You could encapsulate every method that you want to be fluent.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some classes that look like this: MODEL public abstract class BaseEntity<O extends
I'm looking at some Java classes that have the following form: public abstract class
I have some classes that implement a generic base type. i.e: public class TreeItem<TEntity>
I have some simple classes that looks like this: Class Favorites Guid UserId Guid
I have authored some custom classes that I would like to create using XAML:
I have an interface, and some classes that inherit from it. public interface IFoo
I have some methods that are used by several classes. My idea was to
I have some classes in C++ that I would like to expose to Lua.
I have some classes that implement an interface, but also extend the Sprite class:
I have some classes that contains comments like as follow: ... ... ... /*

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.