I got a new MSDN article in the mail last week and I finally got around to reading it. One of the articles was titled "What's New in the .NET Framework 4 Base Class Library" by Justin Van Patten. This article is required reading for any .Net Developer, because whenever a new version of the framework is released it is important for developers to fully understand what has changed. There may be methods and functions that you have avoided over the years due to performance reasons. However, a newer release of the BCL may have addressed that issue and it may be time for you to revisit your code.
For example in the 4.0 release, a lot of System.IO libraries have been revamped. One shining example is the File.ReadAllLines method. In versions prior to .NET 4.0, I would have avoided using this method because when you are dealing with large text files you would have to wait indefinitely for all the lines in the file to be read into memory.
string[] lines = File.ReadAllLines("app.log");
foreach( string line in lines ){
Console.WriteLine( line );
}
As an alternative, I normally use a TextReader to open the file and then read each line of the file into memory. This was generally more efficient and my default practice for dealing with any kind of file parsing.
using( TextReader reader = new StreamReader( "app.log" ) {
string line;
while(( line = reader.ReadLine()) != null ) {
Console.WriteLine( line );
}
}
Well fortunately, this problem has been addressed in the 4.0 libraries. They have overloaded the File.ReadAllLines method so we now have a method call that returns an IEnumerable<string>. Here is an example
IEnumerable<string> lines = File.ReadAllLines("app.log");
foreach( var line in lines ){
Console.WriteLine( line );
}
The .NET 4.0 BCL library has also made optimizations for reading directories and files. Microsoft has done this by introducing new methods which return enumerable types. They also reduced the memory footprint by taking advantage of the underlying OS to read file metadata. Again, by using the enumerable type you no longer will block subsequent operations until all the files are retrieved. This should yield (no pun intended) a great improvement in performance. Here is a list of the new methods
System.IO.File
- public static IEnumerable<string>ReadLines(string path)
- public static void WriteAllLines(string path, IEnumerable<string> contents)
- public static void AppendAllLines(string path, IEnumerable<string> contents)
System.IO.Directory
- public static Enumerable<string> EnumerateDirectories(string path)
- public static IEnumerable<string> EnumerateFiles(string path)
- public static IEnumerable<string> EnumerateFileSystemEntries(string path)
System.IO.DirectoryInfo
- public IEnumerable<DirectoryInfo> EnumerateDirectories()
- public IEnumerable<FileInfo> EnumerateFiles()
- public IEnumerable<FileSystemInfo> EnumerateFileSystemInfos()
In addition to the System.IO improvements we also have support for tuples, parallel extensions and code contracts. All of these items are very important additions to the BCL that you should know about.
Additional Resources: