How to assign more than one Collection in a ObservableCollection using Inline C# Statement? -
i'm having collection of model
public class mobilemodelinfo { public string name { get; set; } public string catagory { get; set; } public string year { get; set; } }
here need assign morethan 1 collection within new observablecollection<mobilemodelinfo>()
using single statement without using moblist.add()
following statement
observablecollection<mobilemodelinfo> moblist = new observablecollection<mobilemodelinfo>( (new mobilemodelinfo { name = "s4", catagory = "smart phone", year = "2011" }), (new mobilemodelinfo { name = "s5", catagory = "smart phone", year = "2013" }), (new mobilemodelinfo { name = "s6", catagory = "ultra smart phone", year = "2015" }) );
but generates compile time error, kindly assist me achieve via inline statement.
note: don't suggest
moblist.add()
an observablecollection
accepts parameters either list<t>
, ienumerable<t>
or nothing.
you forgot declare list before adding items. try this:
observablecollection<mobilemodelinfo> moblist = new observablecollection<mobilemodelinfo>(new list<mobilemodelinfo>(){ new mobilemodelinfo { name = "s4", catagory = "smart phone", year = "2011" }, new mobilemodelinfo { name = "s5", catagory = "smart phone", year = "2013" }, new mobilemodelinfo { name = "s6", catagory = "ultra smart phone", year = "2015" } });
Comments
Post a Comment