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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T18:53:58+00:00 2026-05-22T18:53:58+00:00

I was curious if I could enhance the standard generic List<> functionality because I

  • 0

I was curious if I could enhance the standard generic List<> functionality because I was fed up with writing code such as:

var list = new List<Person>{
     new Person{Name = "David", Age = 24},
     new Person{Name = "John", Age = 30}
 };
list.Add(new Person{Name = "Terry", Age = 28});

I’d prefer that T could be implicitly constructed. The best I came up with allowed me to do this with up to four object construction parameters:

  var list = new ListWithConstructor<string, int, Person>(
                (name,age) => new Person { Name = name, Age = age })
  {
       {"David", 24},
       {"John", 30}
  };          
  list.Add("Terry", 28);

This is implemented like this:

public class ListWithConstructor<T1, T> : List<T>
{
    private readonly Func<T1, T> itemConstructor;
    public ListWithConstructor(Func<T1, T> itemConstructor)
    {
        this.itemConstructor = itemConstructor;
    }

    public void Add(T1 arg1)
    {
        base.Add(itemConstructor(arg1));
    }
}

public class ListWithConstructor<T1, T2, T> : List<T>
{
    private readonly Func<T1, T2, T> itemConstructor;
    public ListWithConstructor(Func<T1, T2, T> itemConstructor)
    {
        this.itemConstructor = itemConstructor;
    }

    public void Add(T1 arg1, T2 arg2)
    {
        base.Add(itemConstructor(arg1, arg2));
    }
}

…and so on for up to four arguments.

Obviously the other List<> constructors (taking a capacity and an IEnumerable of existing elements) can be implemented as well.

How can this be improved?

  • 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-22T18:53:59+00:00Added an answer on May 22, 2026 at 6:53 pm

    The functionality that you are looking for is (almost) already available at your fingertips:

    var names = new[] { "David", "John" };
    var persons = new List<Person>(names.Select(name => new Person { Name = name }));
    

    This way you also have a clear separation of concerns; the List is not at all involved in the object construction (not even by just invoking a delegate), but is simply assigned a sequence of objects. The transformation is taken care of separately.

    This can also handle the case of transforming multiple values into single objects:

    public static IList<Person> GetListFromNamesAndAges(string[] names, int[] ages)
    {
        if (names.Length != ages.Length)
        {
            throw new ArgumentException("names and ages must be of equal length.");
        }
    
        return new List<Person>(
            names.Select((name, index) =>
                new Person { Name = name, Age = ages[index] }));
    }
    
    // usage example:
    var persons = GetListFromNamesAndAges(
        new[] {"David", "John"}, 
        new[] {24, 30});
    

    In the case of merging values from exactly two lists into single objects, using Zip might give slightly cleaner code;

    return names
        .Zip(ages, (name, age) => new Person {Name = name, Age = age})
        .ToList();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm curious about how, running Windows 7 on x86, you could execute some code
I was just curious if I could pass a function as a variable. For
Hi all I was just curious if I could do something like - insert
Hello there i have written some code in jquery to add new input type
Curious how this could be done. Is there any way to create a delegate
Curious if anyone could help out in regards to a Java HotSpot dump...saw some
I was curious if someone could explain to me from a programming perspective what
This problem struck me as a bit odd. I'm curious how you could represent
I was curious if someone could outline which types of WCF contract (interface) changes
I'm curious and it could give my little app a nice finishing touch. Thanks!

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.