Value of Extension Methods

Kane Jeeves

Active member
Joined
May 17, 2006
Messages
44
Programming Experience
5-10
Why would I use an extension method instead of just creating non-extension sub or function?

For ex, I could have an extension function called IsNullOrEmptyOrAllSpaces on String, which does a check as its name implies. Or I can write a stand alone function that does the same thing. Other than having the extension show up in Intellisense, is there any advantage? Is a call to the extension quicker/more efficient than a call to a regular function?
 
From what I have read, it is faster for the compiler to use the extension. I dont know the full reason why, but it does affect compiler speed in the end
 
Correction, its faster because when the compiler pulls everything it needs together to do a build (in the console) it only has to reference one file for the commands. If your building functions outside of it, the console that runs behind vs, has the run through routines to grab all the additional files that you have created and bring them together. I dont know much about how its done, but I have read that in a book called "Mastering Visual Basic 2008".
 
Answered my own question

Well I answered my own question. I was looking for a solution to the problem of adding DBNull's to a command param using .AddWithValue. I didn't want to code like below, since I have well over 100 fields:

If xyz doesn't have a value then
.AddWithValue("xyz", DBNull.Value)
Else
.AddWithValue("xyz", xyz)
End If

What i did was create an extension method called ToValidSQLValue on String that returns the string value trimmed if there's a value, and DBNull.Value if it's empty/null/nothing/spaces. So now all I need is:

.AddWithValue(xyz.ToValidSQLValue)

Very clean. And I created something similar for Nullable Int, etc.
 
Why would I use an extension method instead of just creating non-extension sub or function?

For ex, I could have an extension function called IsNullOrEmptyOrAllSpaces on String, which does a check as its name implies. Or I can write a stand alone function that does the same thing. Other than having the extension show up in Intellisense, is there any advantage? Is a call to the extension quicker/more efficient than a call to a regular function?
Generally, theString.IsNullOrEmptyOrAllSpaces() is better than Utils.IsNullOrEmptyOrAllSpaces(theString) in a OOP perspective. The extension method simply extends the class definition, the primary building block. Mentally you can concentrate on the String and don't have to go out of context to find a utility library that can be used to handle the String. The extension methods feature also has the benefit that you can extend types that you can't change directly because they are defined in someone elses libraries. Typically you don't write an extension method just for any kind of call of a "String", the method should as with a real class member be a logical extension of that type. There's also the idea of reuse, you'd write an extension method if it's something you use a lot, and perhaps add to a tools library that you commonly reference in your projects. Reuse is tied to the OOP concepts of inheritance and polymorphism, something that extension methods can meet by extending general types or generic types.
 
You should also keep in mind that extension methods weren't added to the Framework/language just so that we could write them ourselves. People have just picked up on them because they're handy, but the real reason they exist is to support LINQ. LINQ generally adds extension methods to interfaces rather than classes. If you were to add a method directly to an interface rather than adding an extension method every single class in the world that implemented that interface would now be broken because it doesn't implement that new method. By defining extension methods on an interface you are able to extend every single class that implements that interface without changing any one of those classes. That's the real value of extension methods.
 
Good clarification. This is what I meant by extending 'general types' (base types and interface types), by targeting an interface type the extension method implicitly extends any implemented type of that interface. Remember the extension method provides the implementation, something an interface can not.
 
Good info, thanks! The one thing I think is sorely missing from "new features" documentation is real world examples. Examples usually are trivial and silly.
 
I'm in the throes of my first real ASP.NET MVC project at the moment and the MVC Framework makes heavy use of new features like anonymous types and, indeed, extension methods. In an MVC page you have three properties (Html, Url and Ajax) that are basically objects of types that have predominantly extension methods as "members". For instance, the HtmlHelper class has extension methods that output the string for a text box, a drop-down list and more. Each method basically spits out a bit of HTML code. You're encouraged to define your own extensions to those classes to make your MVC views cleaner. For instance, I've defined extension methods on the UrlHelper class that, given a file name, will spit out the URL for a script file or image file with that name. I've also defined extensions to the HtmlHelper class that spit out the HTML for an image that's a link and a menu item that may or may not be selected. By putting your code into helper extensions you make the code testable, which is a big driver in MVC, and you also make your views much simpler.
 
Back
Top