zoukankan      html  css  js  c++  java
  • .net的一些新语法的整理

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

     

     

  • 相关阅读:
    函数高阶(函数,改变函数this指向,高阶函数,闭包,递归)
    案例:新增数组方法
    案例:商品查询
    案例:forEach和some区别
    ES5新增方法(数组,字符串,对象)
    案例:借用父构造函数继承属性和方法
    构造函数 和 原型
    汽车小常识别让六大汽车驾驶软肋阻碍你
    Opencv 图像增强和亮度调整<6>
    C# StringBulider用法<1>
  • 原文地址:https://www.cnblogs.com/superMay/p/4097398.html
Copyright © 2011-2022 走看看