.NET2.0での慣用句
Learn to Develop with Microsoft Developer Network | MSDN
目立たないですが、既存のクラスライブラリにも色々手が入っています。中には習慣として手が勝手に動くような処理(ファイルの読み込みなど)に関するものもあるので、よく知っているクラスでもクラスリファレンスを見直してみた方が良いかも知れません。
ということで、上記リンクのMSDN TVから何点か取り上げてみます。
using System; using System.Collections.Generic; using System.Text; using System.IO; using System.Diagnostics; namespace ConsoleApplication1 { class Program { static void Main(string args) { Console.WriteLine("文字列がNULLまたは空文字列をチェック"); string s = "Hello"; // .NET 1.1 if (s != null && s != String.Empty) Console.WriteLine(s); // .NET 2.0 if (!String.IsNullOrEmpty(s)) Console.WriteLine(s); Console.WriteLine("ファイルを行単位に読み込む"); // .NET 1.1 using (StreamReader sr = new StreamReader(@"..\..\hello.txt")) { string line = null; while ((line = sr.ReadLine()) != null) Console.WriteLine(line); } // .NET 2.0 foreach (string line in File.ReadAllLines(@"..\..\hello.txt")) Console.WriteLine(line); Console.WriteLine("','区切りで分割、空要素は除外する"); s = "Hello,,World"; // .NET 1.1 foreach (string t in s.Split(',')) { if (t != String.Empty) Console.WriteLine(t); } // .NET 2.0 foreach (string t in s.Split(new char { ',' }, StringSplitOptions.RemoveEmptyEntries)) Console.WriteLine(t); Console.WriteLine("時間を測定する"); // .NET 1.1 DateTime t0; t0 = DateTime.Now; for (int i = 0; i < 10000; ++i) { try { throw new Exception(i.ToString()); } catch (Exception) { } } Console.WriteLine(DateTime.Now - t0); // .NET 2.0 Stopwatch w = new Stopwatch(); w.Start(); for (int i = 0; i < 10000; ++i) { try { throw new Exception(i.ToString()); } catch (Exception) { } } w.Stop(); Console.WriteLine(w.Elapsed); } } } /** 文字列がNULLまたは空文字列をチェック Hello Hello ファイルを行単位に読み込む line1 line2 line3 line1 line2 line3 ','区切りで分割、空要素は除外する Hello World Hello World 時間を測定する 00:00:00.3125000 00:00:00.2958060 */