What am I missing here? I expected the following to work just fine:
public class ProposalFileInfo
{
public int FileId { get; set; }
public bool IsSupportDocument { get; set; }
}
// ...
var attachments = new List<ProposalFileInfo>();
attachments.Add(new ProposalFileInfo { 1, false });
attachments.Add(new ProposalFileInfo { 2, false });
attachments.Add(new ProposalFileInfo { 3, false });
Instead I get an error at the { character on each of the last three lines:
Cannot initialize type ‘xxx.yyy.ProposalFileInfo’ with a collection initializer because it does not implement ‘System.Collections.IEnumerable’
Am I not using an Object initializer? Why does it assume a collection initializer? (I’m using Visual Studio 2012.)
To use an object initializer you have to specify which properties you want to set:
So converting your whole initialization into a collection initializer, we end up with:
However, you could make your code simpler by simply adding a constructor to
ProposalFileInfo:Then your initialization can just be:
If you feel you want to specify what each argument means (or some of them), and you’re using C# 4, you can use named arguments, e.g.