01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
2007-12-17 Mon
■ using [C#]
using 文は D言語の scope(exit) 文に似た機能を持っている。
C# 言語仕様の208ページにから引用
using System; using System.IO; class Test { static void Main() { using (TextWriter w = File.CreateText("log.txt")) { w.WriteLine("This is line one"); w.WriteLine("This is line two"); } using (TextReader r = File.OpenText("log.txt")) { string s; while ((s = r.ReadLine()) != null) { Console.WriteLine(s); } } } }
これを D言語 で書くと、
void main(){ { File w = new File("log.txt",FileMode.Out); scope(exit) w.close(); w.writeLine("This is line one"); w.writeLine("This is line two"); } { File r = new File("log.txt",FileMode.In); scope(exit) r.close; char[] s; while ((s =r.readLine()) != "") { writefln(s); } }
こんな感じだろうか?
finally を書かなくてすむ。
■ 怖いものを見た [C#]
Windows の API に UPnP が用意されている。
COM になっている。
C# から扱う方法を探してみる。
try finally のネストがすごい。
boost::intrusive_ptr みんたいなのがほしい。
COM Interop, Marshal.ReleaseComObject
C++ でつくるかな