vs20008 新特性复习

本文通过实例演示了C#中的LINQ使用方法及其它高级特性,包括对象初始化器、集合初始化器、扩展方法等,并对比了使用委托与lambda表达式的方法。

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(); } } }

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值