Reputation: 718
I have a simple class that has some properties, one property is a list.
public IList<CounterReclaimCardModels> CounterReclaimCards { get; set; }
public long AggID { get; set; }
public string id { get; set; }
I instantiated the class and added some data to it, but I cant figure out how to add data to the list. below is how I instantiated the class
Below is how I am instantiating the list and trying to add to it
CounterReclaim.CounterReclaimCards = new List<CounterReclaimCardModels>()
{
CounterReclaimID = 1,
ReclaimType = "Rule22Pool",
ReclaimDisposition = "None",
};
Upvotes: 1
Views: 192
Reputation: 1118
When creating a list, you can load it up with starting values like so:
CounterReclaim.CounterReclaimCards = new List<CounterReclaimCardModels>
{
new CounterReclaimCardModels()
{
CounterReclaimID = 1,
ReclaimType = "Rule22Pool",
ReclaimDisposition = "None"
},
new CounterReclaimCardModels()
{
CounterReclaimID = 2,
ReclaimType = "AwesomeThing",
ReclaimDisposition = "None"
},
variableForAnExistingCounterReclaimCardModels,
//etc...
};
Or, you can just use the Add method:
CounterReclaim.CounterReclaimCards = new List<CounterReclaimCardModels>();
CounterReclaim.CounterReclaimCards.Add(new CounterReclaimCardModels()
{
CounterReclaimID = 1,
ReclaimType = "Rule22Pool",
ReclaimDisposition = "None"
});
CounterReclaim.CounterReclaimCards.Add(new CounterReclaimCardModels()
{
CounterReclaimID = 2,
ReclaimType = "AwesomeThing",
ReclaimDisposition = "None"
});
CounterReclaim.CounterReclaimCards.Add(variableForAnExistingCounterReclaimCardModels);
Also, you can initialize the list with a few values, then use Add to add more later.
Upvotes: 2
Reputation: 27584
You must create an instance of each item in list:
CounterReclaim.CounterReclaimCards = new List<CounterReclaimCardModels>()
{
new CounterReclaimCardModels
{
CounterReclaimID = 1,
ReclaimType = "Rule22Pool",
ReclaimDisposition = "None"
}
// ...
};
Upvotes: 1
Reputation: 14608
Like so:
CounterReclaim.CounterReclaimCards = new List<CounterReclaimCardModels>()
{
new CounterReclaimCardModels()
{
CounterReclaimID = 1,
ReclaimType = "Rule22Pool",
ReclaimDisposition = "None"
}
};
Upvotes: 2
Reputation: 50825
This will put a single item of type CounterReclaimCardModels
into a new list:
CounterReclaim.CounterReclaimCards = new List<CounterReclaimCardModels>
{
new CounterReclaimCardModels
{
CounterReclaimID = 1,
ReclaimType = "Rule22Pool",
ReclaimDisposition = "None",
}
};
Upvotes: 3