码迷,mamicode.com
首页 > Web开发 > 详细

.net的一些新语法的整理

时间:2014-11-14 17:11:05      阅读:198      评论:0      收藏:0      [点我收藏+]

标签:style   io   ar   os   sp   for   文件   数据   on   

using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks;

namespace a {    public class Program     {         static void Main(string[] args)         {             MyClass mc = new MyClass();             //匿名委托             mc.HowToDoIt(delegate(int a) {                 Console.WriteLine(a);             },10);             //拉姆达表达式             mc.HowToDoIt(a => Console.WriteLine(a), 10);                        //将数据循环出来             List<string> strlist = new List<string> { "aa", "bb" };             strlist.ForEach(a => Console.WriteLine(a));//.net语法糖             foreach (var item in strlist)//普通写法             {                 Console.WriteLine(item);             }

            //将文件写入磁盘中(普通写法)             StreamWriter sw = null;             try             {                 sw = new StreamWriter(@"d:\abcd.txt");                 sw.WriteLine("test");             }             finally             {                 if (sw != null) sw.Dispose();             }             //讲文件写入磁盘(.net语法糖写法)             using (var sws=new StreamWriter(@"d:\abs.txt"))             {                 sws.WriteLine("test");             }             //读取文件内容             using (var sr=new StreamReader(@"d:\abs.txt"))             {                 Console.WriteLine(sr.ReadLine());             }            // 三元表达式             var b = 3;             var c = b > 9?b.ToString():"0"+b;             Console.WriteLine(c);             //两个问号表示,如果左边的是空的话,就等于右边的值,如果右边是空的话就等于左边的值             string aa = "我是aa";             string bb = aa ?? "我是bb";             Console.WriteLine(bb);

            //测试传入的数值是否是数字             var isnum = TestNumber.IsNumber("123");             Console.WriteLine(isnum);

            //匿名类             var li = new             {                 ID="11",name="小红",age=21             };             Console.WriteLine("我是"+li.name+",今年"+li.age+"岁。");

            Console.ReadLine();         }           } }

 

 

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks;

namespace a {    public class MyClass     {        public delegate void DoSomeThing(int a);        public void HowToDoIt(DoSomeThing doMethod,int a) {            doMethod(a);        }

    } }

 

 

 

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Text.RegularExpressions; using System.Threading.Tasks;

namespace a {   public static class TestNumber     {       static private Regex tSnumber = new Regex("\\d+");//实例化一个正则表达式       //将传人的参数与正则表达式匹配       static public bool IsNumber(this string number)       {           if (string.IsNullOrEmpty(number))           {               return false;           }           else           {               return tSnumber.IsMatch(number);           }       }     } }

 

 

.net的一些新语法的整理

标签:style   io   ar   os   sp   for   文件   数据   on   

原文地址:http://www.cnblogs.com/LMzj/p/4097398.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!