vs20008 新特性复习
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(); } } }