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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T00:16:29+00:00 2026-05-13T00:16:29+00:00

The following program will fail because Contracts is not instantiated. Of course I can

  • 0

The following program will fail because Contracts is not instantiated.

Of course I can instantiate it in my constructor but if I have dozens of properties and/or multiple constructors I have to keep track of which are instantiated, etc.

And of course I could create large blocks for these properties with full gets and sets and private field variables, etc. But this gets messy as well.

Isn’t there a way to automatically instantiate these collections with the nice C# property syntax?

using System;
using System.Collections.Generic;

namespace TestProperty232
{
    class Program
    {
        static void Main(string[] args)
        {
            Customer customer = new Customer();
            customer.FirstName = "Jim";
            customer.LastName = "Smith";

            Contract contract = new Contract();
            contract.Title = "First Contract";

            customer.Contracts.Add(contract);

            Console.ReadLine();
        }
    }

    public class Customer
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public List<Contract> Contracts { get; set; }

        public Customer()
        {
            //Contracts = new List<Contract>();
        }
    }

    public class Contract
    {
        public string Title { get; set; }
    }
}
  • 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-13T00:16:29+00:00Added an answer on May 13, 2026 at 12:16 am

    There is no such syntactic sugar, but I’d like to point out a few things:

    • Collection properties should be read-only in any case.
    • If you have a lot of such properties in a single type, it’s a strong symptom that you are violating the Single Responsibility Principle
    • If you have multiple constructors, you should always take great care that there’s only one constructor that does the real work, and all other constructors delegate to that constructor. Alternatively you can delegate all constructor work to a private Initialize method.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

how can I make cases for the following code: win32api.MessageBox(None, This Program Will do
Will the following program cause any problem during compiling and execution process? class A{
In following program . I have one doubt. I have declared one global variable
I have the following program to open lot's of sockets, and hold them open
I have the following program to browse all virtual directories and their sub directories
Hi I have the following program. When I compile on the terminal gcc main.c
In the following program, DummyMethod always print 5. But if we use the commented
In the following code example, the Debug.Assert will fail. If the AsImplementedInterfaces() extension is
In a PyQT4 program, I have a QLabel displaying an image with the following
I have a possibly long running program that currently has 4 processes, but could

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.