-1

我可以在集合初始化程序中插入方法/函数吗?我试图在集合初始化程序中的函数中插入表达式,但类中的变量要求声明它的主体。谢谢你。

            List<demo> ags = new List<demo>()
            {
                new demo { acst = "One", currUpd() = () =>  { intba += totint; } ,  st = 1 < 2},
                new demo { acst = "Two", currUpd() = () =>  { intba += intp; } = 0,  st = 1 < 2 },
                new demo { acst = "Three", currUpd() = () =>  { intba += pp; },  st = 1 < 2 }
            };


            var agss = ags.Select(x => new {acst = x.acst, currUpd = x.currUpd, st = x.st });
            foreach (var item in agss)
            {
                if (item.st == true)
                {
                    item.currUpd();

                }
            }


            public class demo
            {
            public string acst { get; set; }
            public delegate void currUpd() { get; set; }
            public bool st { get; set; }
            }
4

1 回答 1

3

您不能像这样声明委托属性。将其声明为Action

    public Action currUpd { get; set; }

现在你可以这样做:

List<demo> ags = new List<demo>()
{
    new demo { acst = "One", currUpd = () =>  { intba += totint; } ,  st = 1 < 2},
    new demo { acst = "Two", currUpd = () =>  { intba += intp; },  st = 1 < 2 },
    new demo { acst = "Three", currUpd = () =>  { intba += pp; },  st = 1 < 2 }
};

我认为= 0第二个demo是错字

于 2018-01-06T03:47:53.973 回答