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; }
}
}