C# lamda表达式练习实例

用途以及优势:Lambda表达式在什么情况下用呢?  

①当函数体特别简单,只有几个语句,不值当定义一个函数时,用Lambda表达式。

②Lambda表达式不用定义函数名,直接就是参数列表加函数体,对于有起名困难症的程序猿是个福音。

另外不用单独定义函数,代码也更简练。

③ 也是最重要的用法,防止函数名对命名空间的污染。函数名对命名空间的污染以后有机会会单独讲。

练习实例源码:

需加微信交流群的,请加小编微信号zls20210502,切记备注 加群,小编将会第一时间邀请你进群!(目前①群已满 ,需要在②群排队等坑位)

class Program
    {
        private static List<Student> students;
        static void Main(string[] args)
        {
            students  = new List<Student>
                {
                new Student {Name = "Aaa",Age = 17 },
                new Student {Name = "Bbb",Age = 18 },
                new Student {Name = "Ccc",Age = 19 },
                new Student {Name = "Ddd",Age = 10 },
                new Student {Name = "Eee",Age = 100 },
                new Student {Name = "fff",Age = 5 },
                new Student {Name = "gGG",Age = 80 },
                };
            var studentData = students.Where(t => t.Name != null).OrderBy(t => t.Age).ToList();
            studentData = students.OrderBy(t => t.Age).ToList();
            IEnumerable<Student> LineData = from s in students where s.Age > 5 orderby s.Age select s;
            var LineData1 = from s in students where s.Age > 10 orderby s.Age select new { s.Age} ;
            studentData = LineData.ToList();
            foreach(var student in studentData)
            {
                Console.WriteLine($"学生姓名:{student.Name},学生年龄:{student.Age}");
            }
            Console.WriteLine($"\n学生姓名:{studentData.FirstOrDefault().Name},学生年龄:{studentData.FirstOrDefault().Age}");
            Console.WriteLine($"学生姓名:{studentData.Last().Name},学生年龄:{studentData.Last().Age}");
            Console.WriteLine($"\n");
            foreach (var Data1 in LineData1)
            {
                Console.WriteLine($"学生年龄:{Data1.Age}");
            }
            Console.ReadKey();
        }


        public   override string ToString()
        {
            return $"\n学生姓名:{students.FirstOrDefault().Name},学生年龄:{students.FirstOrDefault().Age}";
        }
    }
    /// <summary>
    /// 定义实体类
    /// </summary>
    public class Student
    {
        /// 姓名
        public string Name { get; set; }
        /// 年龄
        public int Age { get; set; }
        /// 默认构造函数(如果提供了其他带参数的构造函数则必须显示申明默认构造函数)
       
 /// 带参数的构造函数
        /// <param name="name">姓名</param>
        /// <param name="age">年龄</param>
        public Student(string name, int age)
        {
            Name = name;
            Age = age;
        }
    }

26412f0b42246586c8219660cdf8c30a.png

下面通过三种实现方式来对对比理解Lambda表达式的用途

假如我们想要从一个整型数组中取出其中是奇数的选项

方法一:命名方法 

public class Common 
{ 
public delegate bool IntFilter(int i); 
public static List<int> FilterArrayOfInt(int[] ints, IntFilter filter) 
{ 
var lstOddInt = new List<int>(); 
foreach (var i in ints) 
{ 
if (filter(i)) 
{ 
lstOddInt.Add(i); 
} 
} 
return lstOddInt; 
} 
}
public class Application 
{ 
public static bool IsOdd(int i) 
{ 
return i % 2 != 0; 
} 
}

调用:

var nums = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; 
var oddNums = Common.FilterArrayOfInt(nums, Application.IsOdd); 
foreach (var item in oddNums) 
{ 
Console.WriteLine(item); // 1,3,5,7,9 
}

方法二:匿名方法 

var oddNums = Common.FilterArrayOfInt(nums, delegate(int i) { return i % 2 != 0; });

方法三:Lambda表达式 

var oddNums = Common.FilterArrayOfInt(nums, i => i % 2 != 0);

很显然,使用Lambda表达式使代码更为简洁。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值