2

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

LittleBobbyTables - Au Revoir
  • 32,008
  • 25
  • 109
  • 114
richard
  • 12,263
  • 23
  • 95
  • 151
  • What is the question in your exam? Difficult to pick correct answer without knowing the question. – Alex Aza May 14 '11 at 21:43
  • "You need to write text to a file. Which of the following demonstrates the most efficient way to use the TextWriter class?" – richard May 14 '11 at 21:46
  • Can't speak to this question in particular, but in my experience, those practice tests are generally just a dump of the questions and are completed by a person and aren't really provided from Microsoft. I know there were several wrong answers in the practice tests I had when I took the MCPD certification course. – Saggio Sep 13 '13 at 13:07

2 Answers2

2

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");
}
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • 1
    Thanks Darin. I just noticed that. I don't even look at the VB examples. I think as far as C# goes, my answer was correct. Agree? – richard May 14 '11 at 21:48
  • @Richard DesLonde, well, given the fact that you didn't have other options, yes, the C# you choose will compile. Whether it's correct is another story. See my update. – Darin Dimitrov May 14 '11 at 21:49
  • Regarding your updated answer, this exam is for .NET 2.0 (70-536) so there are no questions where `var` would be acceptable . . . – richard May 14 '11 at 21:50
  • @Richard DesLonde, oh, damn, who uses .NET 2.0 today :-) I mean even if you had to target .NET 2.0 you could use a decent C# compiler (3.0) which supports the `var` keyword. And in the worst case ever, replace `var` with the type you have on the right hand side. – Darin Dimitrov May 14 '11 at 21:52
  • @Darin: LOL Very true, but I want to start from the ground up to make sure my understanding is solid. Plus the fact that many of my clients still have applications on .NET 2.0 and some even on .NET 1.0!!! I would have taken all the .net 2.0 exams as well but they will be discontinued very soon. :-) – richard May 14 '11 at 21:54
  • Chosen as the answer because he pointed out there was a difference between the two answers, which really cleared up the mystery. For the answer I chose, the C# is obviously more efficient, 3 lines vs their answer with 5 lines. – richard May 14 '11 at 21:56
0

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.

Will A
  • 24,780
  • 5
  • 50
  • 61
  • Hi Will. You are looking at the VB. I'm looking at the C#. :-) I see that you are correct as far as the VB goes, but the VB and C# examples are completely different in the same answer! – richard May 14 '11 at 21:48
  • @Richard - Hi - the highlighted answer (MS's answer) is "the same" for both VB.Net and C#, though. Certainly the fact that on one of the other answers the two differ shouts 'this answer is probably wrong' (and it is wrong!!!!). – Will A May 14 '11 at 21:51
  • LOL Yeah I will be on the lookout for that from now on. But I think on the real exams, you will only get C# questions right? Because you get to choose which language you are targeting? – richard May 14 '11 at 21:52
  • I'd expect you only get one or other - so I guess that strategy doesn't work. Perhaps you need to tell MS you're multilingual so as to get the C#/VB combined paper? :) – Will A May 14 '11 at 21:58
  • @RichardDesLonde Correct, at least for the exams I've taken; you pick whether you want examples to be in VB or C# at the start of the exam. – Saggio Sep 13 '13 at 13:30