Search content within the blog

Wednesday, May 25, 2011

Extension Methods in C#

.NET 3.5 is out which means all of the great features available in C# 3.0 are available to use now. Here's a quick list of the main language enhancements available in C# 3.0:
# Object Initializers
# Automatic Properties
# Extension Methods
# Anonymous Types
# Lambda Expressions
# LINQ
# Collection Initializers

Extension methods allow existing classes to be extended without relying on inheritance or having to change the class's source code. This means that if you want to add some methods into the existing String class you can do it quite easily. Here's a couple of rules to consider when deciding on whether or not to use extension methods:

*Extension methods cannot be used to override existing methods
*An extension method with the same name and signature as an instance method will not be called
*The concept of extension methods cannot be applied to fields, properties or events
*Use extension methods sparingly....overuse can be a bad thing!

Here's an example of creating an extension method in C# that adds a RemoveNonNumeric() method to the String class. Notice that the class is defined as static as well as the extension method itself. The "this" keyword in the parameter signature tells the compiler to add the extension method to the String class since "string" follows the keyword.

namespace StringExtensions
{
public static class StringExtensionsClass
{
public static string RemoveNonNumeric(this string s)
{
MatchCollection col = Regex.Matches(s, "[0-9]");
StringBuilder sb = new StringBuilder();
foreach (Match m in col)
sb.Append(m.Value);
return sb.ToString();
}
}


Here's an example of how the extension method can be used. You'll see that the namespace for the extension method class is imported. From there, the compiler treats the RemoveNonNumeric() method as if it was originally part of the standard System.String class.

using StringExtensions;
.
.
.
.
.
....
string phone = "123-123-1234";
string newPhone = phone.RemoveNonNumeric();

No comments:

Post a Comment