GeekArticles
Database
SQL ServerExtending Base Type Functionality with Extension Methods
Author: aspnet.4guysfromrolla.com |
Published: 6th Dec 2007 |
Visited: 121 times |
Add CommentFiled in: SQL Server
In November 2007, Microsoft released the .NET Framework version 3.5, Visual Studio 2008, and new versions of the
C# and Visual Basic languages (see
An Overview of ASP.NET 3.5 and Visual Studio 2008
for more details on the release). The new C# and Visual Basic versions include a myriad of exciting new features that
make the languages more expressive and open the door for new syntax constructs, like
LINQ. One of these new language features is
extension methods, which is the topic for today's article.
Extension methods allow a developer to tack on her own methods to an existing class in the .NET Framework. For example,
imagine that our developer created a method named StripHtml, that strips HTML elements from a string using a
regular expression. By associating this method with
the System.String class, it could be called as if it was one of the System.String class's
built-in methods:
' VB
Dim str As String = "<b>Hello, world!</b>"
Dim strippedString = str.StripHtml()
// C#
string str = "<b>Hello, world!</b>";
string strippedString = str.StripHtml();
Extension methods make it easy to add functionality to an existing type using a natural syntax. Also, since the extension
methods appear in Visual Studio's IntelliSense drop-down list, they are easier to find and use than wrapper class equivalents.
In this article we will see how to create extension methods in both C# and Visual Basic.
Read on to learn more!
Read More >
Read Article Sponsored Links
Related Articles
• Understanding SAO and CAO Activation Methods in .NET Remoting In this article Abhishek differentiates Server Activated Objects (SAO) and Client Activated Objects (CAO) in .NET Remoting. He examines the implementation procedures of these activation methods separately in Visual Basic 2005 using examples. Abhishek also provides the complete project along with the ...
• Creating XML with the DOM PHP Extension Creating XML with the DOM PHP Extension
In fact, the PHP DOM extension is a set of classes that can be used to generate, access, and manipulate XML data. The DOM.php script defined in the following listing shows how to generate an XML document based on the result set retrieved from the database.
Vi ...
• C++0x Automates Type Deduction with auto Tired of typing unwieldy type names in variable declarations? Why not let the compiler do that dirty job, automatically deducing the type from the variable's initiali ...