Existing Class Enhancements
Not all the enhancements to the BCL are implemented as entirely new classes. After much feedback from clients, Microsoft has started to insert many highly desired improvements to existing BCL classes.
Simplified File Reading and Writing
Reading and writing to a file is easy to accomplish, but certain file access scenarios are not as simplified as they should be. Prior to Whidbey, file access for reading or writing typically involved instantiating a FileStream object with the desired access mode, reading or writing a byte array to or from the file, and then closing the FileStream object. If the file contents consisted of text, the byte array needed to be converted, adding another step. Alternatively, the FileInfo class provided methods to create StreamReader and StreamWriter classes allowing the direct reading or writing of text. Using a FileInfo class and StreamReader or StreamWriter classes added extra steps to the process.
In Whidbey, the File
class gains several shared methods designed to simplify file access. Previously, the File class was used indirectly to access files by providing methods to create FileStream, StreamReader, or StreamWriter classes that perform the actual file manipulations. The File class is now enhanced with six new methods that directly manipulate file contents in a single method call.
All six methods open the file specified by a path
parameter, read or write the data involved, and then close the file. The write methods create the file and overwrite the file if it already exists. The ReadAll
methods operate on a single string, ReadAllBytes
operate on byte arrays, and ReadAllLines
interact with an array of strings. In the following code snippet, the WriteAllLines
method is used to write a string array directly to a file with a single method call:
Improving Graphics Performance
Dim lines(3) As String
lines(0) = "This"
lines(1) = " is"
lines(2) = " demo"
lines(3) = " file"
The new BufferedGraphics class provided in the BCL is intended for manual double buffering scenarios where manual coding and control is desired. For general-purpose double buffering needs, the easiest route to leverage double buffering for Windows Forms or custom controls is built into the Control class found in the System.Windows.Forms namespace. In prior versions of the BCL, double buffering was enabled for controls by using the SetStyle
method of the Control class to set the style bit to True
for three different styles.
Double buffering is now simplified and optimized. The Control class provides a new DoubleBuffered
property and the existing SetStyle
method is used to set a new style bit of the Control class called OptimizedDoubleBuffering. Set either of these values to True
to implement the same behavior as setting the style bits separately for both AllPaintingInWmPaint
. The existing DoubleBuffer
style bit is now deprecated. The OptimizedDoubleBuffering
style behavior redirects all painting operations for the control through a default graphics buffering, greatly reducing flickering without additional code required.