Note that there are some explanatory texts on larger screens.

plurals
  1. POInstantiate List<I> dynamically
    primarykey
    data
    text
    <p>All - </p> <p>I am trying to dynamically (at runtime) want to create a people collection which has 3 attributes - Name, Skillset and Collection of addresses.</p> <p>the problem I am facing is I cant add the address in the following line at the time of instantiate.</p> <pre><code>people.Add(new Person() { Name = "John Smith", Skillset = "Developer", _________ }); </code></pre> <p>So essentially how do i combine these 3 lines into 1 so I can pass it above:</p> <pre><code>Person per = new Person(); per.Complete_Add = new List&lt;Address&gt;(); per.Complete_Add.Add(new Address("a", "b")); </code></pre> <p>Here is my full program:</p> <pre><code>class Program { static void Main(string[] args) { PersonViewModel personViewModel = new PersonViewModel(); } } public class Person { public string Name { get; set; } public string Skillset { get; set; } public List&lt;Address&gt; _address; public List&lt;Address&gt; Complete_Add { get { return _address; } set { _address = value; } } } public class Address { public string HomeAddress { get; set; } public string OfficeAddress { get; set; } public Address(string _homeadd, string _officeadd) { HomeAddress = _homeadd; OfficeAddress = _officeadd; } } public class PersonViewModel { public PersonViewModel() { people = new List&lt;Person&gt;(); Person per = new Person(); \\Dont want to do this per.Complete_Add = new List&lt;Address&gt;(); \\Dont want to do this per.Complete_Add.Add(new Address("a", "b")); \\Dont want to do this people.Add(new Person() { Name = "John Smith", Skillset = "Developer", Complete_Add = per.Complete_Add }); people.Add(new Person() { Name = "Mary Jane", Skillset = "Manager" }); people.Add(new Person() { Name = null, Skillset = null }); } public List&lt;Person&gt; people { get; set; } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload