8.11. Chapter Review#

  1. After writing everything you want to a file through a StreamWriter, what do you still need to remember to do?

  2. If you want to create a file path in an operating system independent way for file f.txt in directory d2, which is a subdirectory of d1, which is a subdirectory of the current directory, how would you do it?

  3. Windows uses \ as a path separator. If you want to write a literal directly for a Windows path, what issue is there in C#?

  4. In a file path, how do you refer to the parent directory of the current directory, without using the actual name of the parent directory?

  5. If you are reading from a StreamReader inFile (namely, for example, you wrote: StreamReader inFile = new StreamReader(path))), what is logically wrong with the following:

   if (inFile.ReadLine().Contains("!")) {
      Console.WriteLine(inFile.ReadLine() + "\n contains the symbol !")
   }

8.12. Chapter Review#

8.12.1. Key takeaways#

  • Clean text before parsing.

  • Validate structure before converting types.

  • Use TryParse for robust conversion.

  • Handle malformed lines without crashing.

  • Keep original text when doing replacements.

8.12.2. Review questions#

  1. What is the difference between text normalization and parsing?

  2. Why should TryParse be preferred over Parse for user/file input?

  3. What checks should occur before indexing parts[2] after Split(',')?

  4. Why is preserving the original line useful during cleanup?

8.12.3. Practice prompt#

Write a method that accepts one CSV-like line and returns:

  • whether parsing succeeded

  • normalized name

  • parsed score

Use out parameters and do not throw on malformed input.