一个关于TextWriter的问题。
这是MCTS 70-536的一个考题,
You need to write text to a file. Which of the following demonstrates the most efficient way to use the TextWriter class?
A:
// C#
TextWriter tw = new TextWriter( "Hello.dat ");
tw.Write( "Hello, world! ");
tw.Close();
B:
// C#
FileStream fs = new FileStream( "Hello.dat ", FileMode.Create);
TextWriter tw = new StreamWriter(fs);
tw.Write( "Hello, world! ");
tw.Close();
fs.Close();
C:
// C#
TextWriter tw = new StreamWriter( "Hello.dat ");
tw.Write( "Hello, world! ");
tw.Close();
D:
// C#
TextWriter tw = new FileStream( "Hello.dat ", FileMode.Create);
tw.Write( "Hello, world! ");
tw.Close();
答案是B,解释是
The TextWriter class does not have a constructor. Instead, you should create it using the StreamWriter constructor. To create a StreamWriter object, you must use an existing Stream object, such as an instance of FileStream.
问题是StreamWriter是可以用文件名来直接构建对象的,C为什么不对呢??我测试也没有问题的。
------解决方案--------------------StreamWriter是TextWriter的派生类
估计是因为必须的用TextWriter的实例