MS 考试 70-536 准备:有效使用 TextWriter - 练习测试答案是错误的(我认为)!

发布于 2024-11-06 22:22:02 字数 382 浏览 4 评论 0原文

我正在参加 70-536 考试的模拟考试。下面是一个屏幕截图。黄色突出显示的是考试所说的正确答案。选中单选按钮的那个就是我认为的答案。

请注意底部的解释,其中包括以下语句:

要创建 StreamWriter 对象,您必须使用现有的 Stream 对象,例如 FileStream 的实例。

我认为我选择的答案是最有效的用途,并且我认为解释中的陈述是错误的。显然,因为我选择的答案中的代码运行良好。

谁说得对???

在此处输入图像描述

I'm taking a practice test for the exam 70-536. Below is a screenshot. The yellow highlighted is what the exam says is the correct answer. The one with the radio button selected is the answer I thought it was.

Note the explanation at the bottom which includes the statement:

To create a StreamWriter object, you must use an existing Stream object, such as an instance of FileStream.

I think the answer I chose is the most efficient use and I think the statement made in the explanation is wrong. Clearly, because the code in my selected answer worked fine.

Who's right????

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

快乐很简单 2024-11-13 22:22:02

在您选择的答案中,C# 和 VB.NET 版本之间存在差异。 VB.NET 版本甚至无法编译,而 C# 版本是正确的。

这不会编译:

Dim tw as TextWriter = New FileStream("Hello.dat", FileMode.Create)

这没关系:

TextWriter tw = new StreamWriter("Hello.dat");

最后一个答案是不可能的,因为 TextWriter 是一个抽象类,你不能直接实例化它。

但显然,您在现实应用程序中使用的正确答案甚至不存在于列表中。它将是:

using (var writer = new StreamWriter("Hello.dat"))
{
    writer.Write("Hello world");
}

或者如果您需要使用 Stream

using (var stream = File.Create("Hello.dat"))
using (var writer = new StreamWriter(stream))
{
    writer.Write("Hello world");
}

In the answer you choose there's a difference between the C# and VB.NET version. The VB.NET version won't even compile whereas the C# is correct.

This won't compile:

Dim tw as TextWriter = New FileStream("Hello.dat", FileMode.Create)

This is OK:

TextWriter tw = new StreamWriter("Hello.dat");

The last answer is out of the question because TextWriter is an abstract class and you cannot instantiate it directly.

But obviously the correct answer which is what you would use in a real world application is not even present in the list. It would be:

using (var writer = new StreamWriter("Hello.dat"))
{
    writer.Write("Hello world");
}

or if you need to use a Stream:

using (var stream = File.Create("Hello.dat"))
using (var writer = new StreamWriter(stream))
{
    writer.Write("Hello world");
}
度的依靠╰つ 2024-11-13 22:22:02

他们是对的 - 您不能将 TextWriter 设置为等于 FileStream 的实例,因为 FileStream 不是从 TextWriter 继承的 - 您需要使用基于 FileStream 的 StreamWriter,因为 StreamWriter 确实继承自 TextWriter。

They're right - you can't set a TextWriter equal to an instance of FileStream as FileStream does not inherit from TextWriter - you need to use a StreamWriter based on the FileStream as StreamWriter does inherit from TextWriter.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文