static void Main(string[] args) { //lamda表达式 项目中使用的好多 //goes to =>滚向 //使用匿名函数实现 DelOne del = delegate() { Console.WriteLine("匿名函数的使用"); }; //使用lamda表达式实现:表示方法参数,表示方法体 DelOne del1 = () => { }; DelTwo del2 = (string name) => { }; DelThree del3 = (string name) => { return(name); }; //匿名函数的调用和执行 del(); del1(); del2("陈如水"); del3("陈如水"); List <int> list = new List <int>() { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; list.RemoveAll(n => n > 4); foreach (var item in list) { Console.WriteLine(item); } Console.ReadKey(); }
static void Main(string[] args) { Delegatos delegator = new Delegatos(); DelOne dele1 = delegator.Uno; DelTwo dele2 = delegator.Dos; DelThree dele3 = delegator.Tress; dele1(); Console.WriteLine(dele2()); dele3("1", "2", 3); Console.ReadLine(); }
static void Main(string[] args) { DelOne del = () => { }; //delegate() { }; DelTwo del2 = (int num) => { }; //delegate(int num) { }; DelThree del3 = (string name) => { return(name); }; //delegate(string name) { return name; }; //例如数组中的条件也可以用表达式表示 List <int> nums = new List <int> { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; //表达式 n => n>3 nums.RemoveAll(n => n > 3); }
static void Main(string[] args) { DelOne del = () => { }; //delegate(){ }; DelTwo del2 = (string name) => { }; //delegate(string name){ }; DelThree del3 = (string name) => { return(name); }; //delegate(string name){return name}; List <int> list = new List <int>() { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; list.RemoveAll(n => n > 4); foreach (var item in list) { Console.WriteLine(item); } Console.ReadKey(); }
static void Main(string[] args) { DelOne del = () => { }; //delegate() { }; DelTwo del2 = (string name) => { }; //delegate(string name) { }; DelThree del3 = (string name) => { return(name); }; List <int> list = new List <int>() { 4, 8, 12, 24, 5, 9, 6 }; list.RemoveAll(n => n > 6); //for (int i = 0; i < list.Count; i++) //{ // Console.WriteLine(list[i]); //} foreach (var item in list) { Console.WriteLine(item); } }
static void Main(string[] args) { DelOne one = () => { }; DelTwo two = (string name) => { }; DelThree three = (string name) => { return(name); }; }