8.11. Chapter Review#
After writing everything you want to a file through a
StreamWriter, what do you still need to remember to do?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?
Windows uses
\as a path separator. If you want to write a literal directly for a Windows path, what issue is there in C#?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?
If you are reading from a
StreamReaderinFile(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
TryParsefor robust conversion.Handle malformed lines without crashing.
Keep original text when doing replacements.
8.12.2. Review questions#
What is the difference between text normalization and parsing?
Why should
TryParsebe preferred overParsefor user/file input?What checks should occur before indexing
parts[2]afterSplit(',')?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.