Spontaneous Publicity
blogs are the new phone book

Some of My Favorite Extension Methods

March 24, 2008 11:24 by Luke

There has been some discussion about extension methods and how they should be used and when. Personally, I try to use them when it will increase the readability of my code. A common candidate for extension methods is the static helper method. These mostly take the form of something like:

public static class StringHelper
{
     public static string Truncate(string s, int maxLength);
}

And is usually consumed with something like:

string newString = StringHelper.Truncate(myString, 10);

Now, this isn't too bad, but with extension methods, the signature for this method now looks like:

public static string Truncate(this string s, int maxLength)

and is used with syntax like the following:

string newString = myString.Truncate(10);

So I hope you agree that this improves readability and understanding. And it also prevents you from sprinkling "MyHelperClass.MyMethod" calls everywhere

I thought I would share some of the extension methods that I am finding invaluable.

RenderControl

This method allows you to take and WebControl and render it as a string. This is very useful for html code generation. The method signature for the helper method is:

public static string RenderControl(WebControl control);

Since the syntax to use extension methods is slightly different than static methods, I decided to rename this method to RenderAsString:

publicstatic string RenderAsString(this WebControl control)
{
    StringBuilder sb = new StringBuilder();
    using (StringWriter sw = new StringWriter(sb))
    {
        using (HtmlTextWriter tw = new HtmlTextWriter(sw))
        {
            control.RenderControl(tw);
            return sb.ToString();
        }
    }
}

GetTableCell

This method has many overloads so you can get all of them in the attached download. I will just show one here:

public static void Add(this TableCellCollection cells,
                       string text, string cssClass, int? columnSpan)
{
    TableCell cell = new TableCell();
    cell.Text = text;
    if (!String.IsNullOrEmpty(cssClass))
        cell.CssClass = cssClass;
    if (columnSpan.HasValue)
        cell.ColumnSpan = columnSpan.Value;
    cells.Add(cell);
}

The Syntax for using this is something like:

Table table = new Table();
TableRow tr = new TableRow();
tr.Cells.Add("My Cell", "MyCssClass", 2);

Which is much better than:

tr.Cells.Add(HtmlHelper.GetTableCell("My Cell", "MyCssClass", 2));

These are just a couple of small examples of how I am finding Extension methods a great tool for making code more readable and concise. You can download the source code for both of these methods here:

kick it on DotNetKicks.com

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5
Tags:
Categories:
Actions: E-mail | Permalink | Comments (3) | Comment RSSRSS comment feed

Related posts

Comments

March 24. 2008 17:30

John S.

while you're at it, you should write string.HasValue() (maybe extension properties will make it in to C# 4.0 so we can ditch the extra parens):

public static bool HasValue(this string s)
{
return s != null;
}

then your table cell code can look like this:
if (cssClass.HasValue())
cell.CssClass = cssClass;
if (columnSpan.HasValue)
cell.ColumnSpan = columnSpan.Value;

John S.

April 2. 2008 21:16

Pepe

Amazing post!!! Thnx for sharing

Pepe

April 4. 2008 00:40

Gary

One of my favorites... so simple:

public static bool IsNullOrEmpty(this string s)
{
return String.IsNullOrEmpty(s);
}


if (String.IsNullOrEmpty(myString))
// do something

becomes:

if(myString.IsNullOrEmpty)
// do something

Gary

Add comment


(Will show your Gravatar icon)  

  Country flag

[b][/b] - [i][/i] - [u][/u]- [quote][/quote]



Live preview

October 13. 2008 12:47