Using extension methods in C#

One of the most exciting features of the .NET Framework is Extension Methods. Extension methods allow you to extend the functionality of a class without modifying the code of the class itself. This allows you to write methods for a class anywhere outside the class. This is especially useful when there is no access to the code of the original class. For example, you can write a method to extend the System.String class without actually changing the code of the System.String class. (You can't actually change the code of the System.String class) This method will be a static method residing in a static class anywhere in your application.

Extension methods are defined as static methods, but are invoked as instance methods. This is achieved by prefixing the this keyword to the argument passed in the static method. For example, an extension method meant to remove vowels in a string would have a definition like:
public static string RemoveVowels(this string str)
and it would be invoked as:

  1. string str = "Hello";
  2. string newstr = str.RemoveVowels();

A better, more complete example should make matters clearer. In an earlier post, the DirectoryInfo class has been explained, and as has been pointed out, it does not have a method or property which returns its byte size unlike the FileInfo class. So, one can write a method to extend the DirectoryInfo class as under:

  1. public static class ExtensionMethods
  2. {
  3. public static long GetSize(this DirectoryInfo dir)
  4. {
  5. // Initialize to zero
  6. long size = 0;

  7. // Loop through all the subdirectories and files
  8. foreach(DirectoryInfo folder in dir.GetDirectories())
  9. size += folder.GetSize(); // Recursive call
  10. foreach(FileInfo file in dir.GetFiles())
  11. size += file.Length;

  12. return size;
  13. }
  14. }

So one can use this method as an instance method of any DirectoryInfo instance.

  1. DirectoryInfo dir = new DirectoryInfo(@"C:\WINDOWS");
  2. long sizeOfWindowsFolder = dir.GetSize();

Visual Studio 2008 provides IntelliSense support for extension methods, which makes the job of using extension methods very easy. Programmers vary in opinion about the concept of extension methods with most programmers terming it as "advantageous" while some programmers terming it as "a slap in the face of all serious software programmers". I say, as is with everything in technology, it's all up to you, sir!


A brief introduction to WPF

My previous post was an out-of-nowhere post on WPF (Windows Presentation Foundation), a technology not many developers know. Although you can easily know all there is to know "about" WPF on the web, learning to "use" WPF is more important and you need the proper resources for that. WPF applications can be developed in both Visual Studio 2008 and Expression Blend 2 and developing a complete WPF app requires expertise in both. Here are some resources which you can use:

A guided tour of WPF

The official WPF site

The MSDN resource for WPF

Here's my description of WPF:
WPF (Windows Presentation Foundation) is a programming model within the .NET Framework which allows you to build rich, compelling user interfaces. The highly-rated Silverlight platform for the Web is nothing but a subset of WPF. You can compare it to Windows Forms, although there is a world of difference between the two. In fact, WPF is best understood when compared to WinForms.
In a WinForms app, there are 1 or more "forms" which constitute the user interface. Each form is an instance of a class which derives from System.Windows.Forms.Form and its UI and codebehind are both in C#. So, there is a Form1.designer.cs and a Form1.cs both containing partial declarations of the class Form1.
In a WPF app, there are 1 or more "windows" which constitute the interface. Each window is an instance of a class which derives from System.Windows.Window. While the codebehind is in C#, the UI is in what is called XAML (Extensible Application Markup Language). So, there is a Window1.xaml containing pure XAML and a Window1.xaml.cs containing pure C#, both containing partial declarations of the class Window1.
XAML is an HTML-style language which allows you to layout the controls of a window just as you would layout elements on an HTML page. XAML provides what WinForms cannot provide - rich and highly customizable user experiences. XAML provides for custom ListBoxes, binding controls to XML and CLR data sources, animations through storyboards and plenty more. In fact, you will get a better idea once you actually start creating a WPF app in Visual Studio 2008.
Apart from Visual Studio 2008, there is a software called Expression Blend 2 which allows you to concentrate only on the UI through XAML. Its intuitive interface allows you to do whatever you wish with the user interface. WPF is a very good and recommended thing to learn for developers. Code hard, go pro!