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

  • Home
  • SEARCH
  • 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 7191731
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T19:47:31+00:00 2026-05-28T19:47:31+00:00

I have a type: public class Human { public int Id { get; set;

  • 0

I have a type:

public  class Human
{
    public int Id { get; set; }
    public string Address { get; set; }
    public string Name { get; set; }
    public List<ContactNumber> ContactNumbers { get; set; }

    public Human(int id)
    {
        Id = id;
    }

    public Human(int id, string address, string name,
                 List<ContactNumber> contactNumbers) :
        this(id)
    {
        Address = address;
        Name = name;
        ContactNumbers = contactNumbers;
    }        
}

Please guide me is among best practices to use constructor with for list initialization?
How to initialize a list using constructor?

Edit:

Please guide me is among best practices to use constructor with for list initialization?
How to assign values to list using a constructor, so if no value passed a default will be used?

Thanks

  • 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-28T19:47:32+00:00Added an answer on May 28, 2026 at 7:47 pm

    Using a collection initializer

    From C# 3, you can use collection initializers to construct a List and populate it using a single expression. The following example constructs a Human and its ContactNumbers:

    var human = new Human(1, "Address", "Name") {
        ContactNumbers = new List<ContactNumber>() {
            new ContactNumber(1),
            new ContactNumber(2),
            new ContactNumber(3)
        }
    }
    

    Specializing the Human constructor

    You can change the constructor of the Human class to provide a way to populate the ContactNumbers property:

    public class Human
    {
        public Human(int id, string address, string name, IEnumerable<ContactNumber> contactNumbers) : this(id, address, name)
        {
            ContactNumbers = new List<ContactNumber>(contactNumbers);
        }
    
        public Human(int id, string address, string name, params ContactNumber[] contactNumbers) : this(id, address, name)
        {
            ContactNumbers = new List<ContactNumber>(contactNumbers);
        }
    }
    
    // Using the first constructor:
    List<ContactNumber> numbers = List<ContactNumber>() {
        new ContactNumber(1),
        new ContactNumber(2),
        new ContactNumber(3)
    };
    
    var human = new Human(1, "Address", "Name", numbers);
    
    // Using the second constructor:
    var human = new Human(1, "Address", "Name",
        new ContactNumber(1),
        new ContactNumber(2),
        new ContactNumber(3)
    );
    

    Bottom line

    Which alternative is a best practice? Or at least a good practice? You judge it! IMO, the best practice is to write the program as clearly as possible to anyone who has to read it. Using the collection initializer is a winner for me, in this case. With much less code, it can do almost the same things as the alternatives — at least, the alternatives I gave…

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

Sidebar

Related Questions

I have a type like this: public class EffectViewModel { public string Name {
I have some custom type: [RdfSerializable] public class Item { [RdfProperty(true)] public string Name
Lets say have this immutable record type: public class Record { public Record(int x,
I have a type: public class IssueForm { Order Order {get; set;} Item Item
There is the following type public class Nested1 { public int Id {get; set;}
I have a specialized list that holds items of type IThing : public class
I have a type like this: public class TypeValue { public Type Type {
say i have this class Framework.Asd.Human with a public empty constructor. and i want
I have this custom type: public struct PasswordString { private string value; public PasswordString(string
I have a complex type entity public class ComplexEntity : ComplexObject { private int

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.