標籤: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的一些新文法的整理