If it is so similar in signature why doesnt console

Info icon This preview shows pages 422–424. Sign up to view the full content.

View Full Document Right Arrow Icon
If it is so similar in signature, why doesn’t Console derive from Text Writer ? TextWriter is intended to be used with some underlying resource that needs proper lifetime management, so it implements IDisposable . Our code would be much less readable if we had to wrap every call on Console with a using block, or remember to call Dispose —especially as it isn’t really necessary. So, why make TextWriter implement IDisposa ble ? We do that so that our text-writing code can be implemented in terms of this base class, without needing to know exactly what sort of TextWriter we’re talking to, and still handle the cleanup properly. The File class’s CreateText method calls a constructor on StreamWriter which opens the newly created file, and makes it ready for us to write; something like this: return new StreamWriter(fullPath, false); There’s nothing to stop you from doing this yourself by hand, and there are many situations where you might want to do so; but the helper methods on File tend to make your code smaller, and more readable, so you should consider using those first. We’ll look at using Stream Writer (and its partner, StreamReader ) in this way later in the chapter, when we’re dealing with different sorts of underlying streams. Hang on, though. We’ve snuck a second parameter into that constructor. What does that Boolean mean? When you create a StreamWriter , you can choose to overwrite any existing file content (the default), or append to what is already there. The second Boo- lean parameter to the constructor controls that behavior. As it happen, passing false here means we want to overwrite. This is a great example of why it’s better to define nicely named enu- merations, rather than controlling this sort of thing with a bool . If the value had not been false , but some mythical value such as OpenBehav ior.Overwrite , we probably wouldn’t have needed to explain what it did. C# 4.0 added the ability to use argument names when calling methods, so we could have written new StreamWriter(fullPath, append: false) , which improves matters slightly, but doesn’t help you when you come across code that hasn’t bothered to do that. 398 | Chapter 11: Files and Streams
Image of page 422

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
So, now we can easily complete the implementation of our CreateFile method, as shown in Example 11-22 . Example 11-22. Writing a string with StreamWriter private static void CreateFile(string fullPath, string p) { using (StreamWriter writer = File.CreateText(fullPath)) { writer.Write(p); } } We just write the string we’ve been provided to the file. In this particular application, Example 11-22 isn’t an improvement on Example 11-20 —we’re just writing a single string, so WriteAllText was a better fit. But StreamWriter is an important technique for less trivial scenarios.
Image of page 423
Image of page 424
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern