.net的一些新文法的整理

來源:互聯網
上載者:User

標籤: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+");//執行個體化一個Regex       //將傳人的參數與Regex匹配       static public bool IsNumber(this string number)       {           if (string.IsNullOrEmpty(number))           {               return false;           }           else           {               return tSnumber.IsMatch(number);           }       }     } }

 

 

.net的一些新文法的整理

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.