开发者

What is the way for direct assignment?

What is the way to add items to list when i have class setup

class person
    {
        string name;
        List<RecipeList> rcp = new List<RecipeList>();
        public List<RecipeList> RList
        {
            get { return rcp; }
            set { rcp = value; }
        }
        public string Name
        {
            get{return name;}
            set {name=value;}
        }
    }



   class RecipeList
    {
        string nameofrec;
        public string NameofRecipe
        {
            get { return nameofrec; }
            set { nameofrec = value; }
        }
    }

following declaration not working

person[] prn = new person[]

 { new person { Name = "Robert",
    RList=new List<RecipeList>().AddRange("开发者_如何学GoCoak","Pizza")},

 new person { Name = "Rahim",
              RList =new List<RecipeList>().AddRange("Coak","OnionBread")}
 };


The RList isn't a list of strings - you need to use ReceipeList objects, like below; note I'm using the fact that the list (rcp) has a field-initializer (you could actually remove the set and it would still work, as long as you aren't using XmlSerializer):

person[] prn = new person[]
{
    new person { Name = "Robert", RList = {
      new RecipeList { NameofRecipe = "Coak" },
      new RecipeList { NameofRecipe = "Pizza" }
    } },
    new person { Name = "Rahim", RList = {
      new RecipeList { NameofRecipe = "Coak" },
      new RecipeList { NameofRecipe = "OnionBread" }
    } },
};

Note that you could simplify this with a few bespoke constructors...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜