Tech Off Post

Single Post Permalink

View Thread: Need C# help
  • User profile image
    Sven Groot

    To follow up a little, this is genuinely something that I struggle with. I understand the need to keep example code simple. I understand the need not to overwhelm learners with too many new concepts at once. But I'm also of the opinion that this leads to people learning bad habits. C++/Win32 sample code is especially guilty of this; error handling in C++ (if not using exceptions like when you're using Win32 or COM) is so verbose that it nearly always gets omitted from sample code, and how many bugs in released products are caused because someone ignored an error somewhere they shouldn't have?

    In this particular case I'd probably be in favour of just including the using statement even if it isn't explained. Even if there's just a note saying "trust us, you need to do this whenever you deal with some things like streams, we'll explain why later." That may not be the best approach, but I think it's better than creating a sample for someone to copy that does the wrong thing. And since the sample does include a call to Close, it's clearly not averse to teaching the concept of having to close a file. All it needs to do is say "the using statement makes sure the file is closed no matter what, even if your program crashes" which should be fairly clear even if you haven't explained exceptions yet.