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 7906731
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T11:06:44+00:00 2026-06-03T11:06:44+00:00

this is what I’m trying to achieve: config.Name(Foo) .Elements(() => { Element.Name(element1).Height(23); Element.Name(element2).Height(31); })

  • 0

this is what I’m trying to achieve:

config.Name("Foo")
      .Elements(() => {
                         Element.Name("element1").Height(23);
                         Element.Name("element2").Height(31);
                      })
     .Foo(23);

or like this:

  .Elements(e => {
                     e.Name("element1").Height(23);
                     e.Name("element2").Height(31);
                  })
  .Foo(3232);

this is what I have for the moment:

public class Config
{
   private string name;
   private int foo;
   private IList<Element> elements = new List<Element>();

   public Config Name(string name)
   {
      this.name = name;
      return this;
   }

   public Config Foo(int x)
   {
       this.foo = x;
   }

   ... //add method for adding elements 

   class Element
   {
      public string Name { get; set; }
      public int Height { get; set; }
   }
}

anybody knows how to do this ?

  • 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-06-03T11:06:46+00:00Added an answer on June 3, 2026 at 11:06 am

    Any reason you don’t want to use object and collection initializers?

    public class Config
    {
       public string Name { get; set; }
       public int Foo { get; set; }
       public IList<Element> Elements { get; private set; }
    
       public Config()
       {
           Elements = new List<Element>();
       }
    }
    
    // I'm assuming an element *always* needs a name and a height
    class Element
    {
       public string Name { get; private set; }
       public int Height { get; private set; }
    
       public Element(string name, int height)
       {
           this.Name = name;
           this.Height = height;
       }
    }
    

    Then:

    var config = new Config
    {
        Name = "Foo",
        Elements = { 
            new Element("element1", 23),
            new Element("element2", 31)
        },
        Foo = 23
    };
    

    If you don’t want to expose the list of elements directly, you could always turn that into a builder, and copy it into a more private data structure on Build:

    var config = new Config.Builder
    {
        Name = "Foo",
        Elements = { 
            new Element("element1", 23),
            new Element("element2", 31)
        },
        Foo = 23
    }.Build();
    

    This has the additional advantage that you can make Config itself immutable.

    If you always need Name to be present, just take that as a constructor parameter instead.

    While there are times where it’s good to have a fluent interface with mutating (or copy-and-change) method calls, in this case I think collection/object initializers are more idiomatic C#.

    Note that if you’re using C# 4 and you want to make your Element constructor calls, you can always use named arguments:

    new Element(name: "element2", height: 31)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have some data like this: 1 2 3 4 5 9 2 6
This is really just for my own use: I would like to be able
This has been a rather problematic issue on numerous occasions. We have alot of
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
I am trying to render a haml file in a javascript response like so:
This might seem like a stupid question I admit. But I'm in a small
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this

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.