using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace TestLinq { public class Point { //1.自动属性 public int X { get; set; } public int Y { get; set; } } class Program { static void Main(string[] args) { //2.对象初始化器 Point p = new Point { X = 2, Y = 3 }; //3.集合初始化器 List<int> lst = new List<int> { 1, 3, 5, 2, 2, 1, 4, 111, 2, 3222 }; //4.扩展方法 string s = "1"; int i = s.ToInt32(); Console.WriteLine(i); string noPascal = "welcome to linq"; Console.WriteLine("Pascal:" + noPascal.ToPascal()); //得到所有包含2的数字的集合---a.用委托 var lst1 = lst.FindAll( delegate(int n) { return n.ToString().IndexOf("2") != -1; } ); //得到所有包含2的数字的集合---b.用lambda表达式 var lst2 = lst.FindAll((ele) => { return ele.ToString().IndexOf("2") != -1; } ); foreach (var j in lst1) { Console.WriteLine(j); } Console.ReadLine(); } } static class T { //4.扩展方法 public static int ToInt32(this string s) { return Convert.ToInt32(s); } public static string ToPascal(this string str) { StringBuilder result = new StringBuilder(); string[] arr = str.Split(' '); foreach (string s in arr) { result.Append(s.Length > 1 ? s.Substring(0, 1).ToUpper() + s.Substring(1) : s.ToUpper()); } return result.ToString(); } } }
vs20008 新特性复习
最新推荐文章于 2022-09-21 19:41:25 发布
本文通过实例演示了C#中的LINQ使用方法及其它高级特性,包括对象初始化器、集合初始化器、扩展方法等,并对比了使用委托与lambda表达式的方法。
17万+

被折叠的 条评论
为什么被折叠?



