I am trying to find out the syntax to add a complex object into a list. Here’s what I know works:
ComplexObject temp = new ComplexObject() {attribute1 = 5, attribute2 = 6};
ComplexObjectList.Add(temp);
Here’s what I’m wanting to do instead:
ComplexObjectList.Add(new ComplexObject(){attribute1 = 5, attribute2 = 6});
I realize that it doesn’t work, but is there any other way to add one to my list without creating one before hand? I have no need for temp other than this one function call.
Your code should work. If not, then the problem is probably in the declaration of ComplexObjectList.
Should be:
List<ComplexObject> ComplexObjectList = new List<ComplexObject>();Also make sure the class properties are public.