asp net 3.5unleashed

Page 430

as long as the variables are all of the same type. It also supports the capability to just use an already initialized variable. For more information about this topic, see the C# Programmer’s Reference. I’m not aware of any other programming language that offers a similar convenient syntax for using types that implement the dispose pattern. In other programming languages, you must manually code the try and finally exception handling frames. Important As I stated at the end of the previous section, you should call Dispose or Close only in situations where you need the object to be cleaned up before the next statement executes. For this reason, you should use C#’s using statement cautiously. I’ve seen many developers use C#’s using statement liberally, only to find out later that they have been explicitly cleaning up objects too early, causing another part of their application to throw ObjectDisposedException exceptions.

An Interesting Dependency Issue The System.IO.FileStream type allows the user to open a file for reading and writing. To improve performance, the type’s implementation makes use of a memory buffer. Only when the buffer fills does the type flush the contents of the buffer to the file. A FileStream supports the writing of bytes only. If you want to write more complex data types (such as an Int32, a Double, a String, and so on), you can use a System.IO.BinaryWriter as demonstrated in the following code: FileStream fs = new FileStream("DataFile.dat", FileMode.Create); BinaryWriter bw = new BinaryWriter(fs); bw.Write("Hi there");

// The following call to Close is what you should do. bw.Close(); // NOTE: BinaryWriter.Close closes the FileStream. The FileStream // shouldn’t be explicitly closed in this scenario. Notice that the BinaryWriter’s constructor takes a reference to a FileStream object as a parameter. Internally, the BinaryWriter object saves the FileStream’s reference. When you write to a BinaryWriter object, it internally buffers the data in its own memory buffer. When the buffer is full, the BinaryWriter object writes the data to the FileStream. When you’re done writing data via the BinaryWriter object, you should call Dispose or Close. (Because the BinaryWriter type implements the dispose pattern, you can also use it with C#’s using statement.) Both of these methods do exactly the same thing: cause the BinaryWriter object to flush its data to the FileStream object and close the FileStream object. When the FileStream object is closed, it flushes its buffer to disk just prior to calling the Win32 CloseHandle function. Note You don’t have to explicitly call Dispose or Close on the FileStream object because the BinaryWriter calls it for you. However, if you do call Dispose/Close explicitly, the FileStream will see that the object has already been cleaned up—the methods do nothing and just return.


Issuu converts static files into: digital portfolios, online yearbooks, online catalogs, digital photo albums and more. Sign up and create your flipbook.