I recently found out about C# extension methods and wrote this one:
/// <summary>
/// Short-hand for setting the data source and binding it.
/// </summary>
/// <param name="me">The control to set and bind the data source of.</param>
/// <param name="dataSource">The data source.</param>
public static void BindTo(this DataBoundControl me, IEnumerable dataSource)
{
me.DataSource = dataSource;
me.DataBind();
}
What do you guys think? Is this a reasonable extension method to use in a professional ASP.NET project?
Not really. It saves you 1 method call, at the result of introducing an unknown approach. And you really shouldn’t need to be setting the binding all the time anyway (depending on how you do it, I suppose, I prefer to do it in the markup).
So no, I wouldn’t consider this appropriate. But then again, I’m quite against extension methods in general, so I’m clearly biased, FWIW.