Monday, April 11, 2011

Ex-tension

There is a simple rule of thumb in which namespace extensions methods should be put. If this is a general purpose extension method - put it in the same namespace as 'this' parameter:

namespace System
{
 public static class StringExtensions
 {
  public static string Fmt(this string format, params object[] args)
  {
   return string.Format(format, args);
  }
 }
}

If an extension method use some specific parameters, but still is more or less general purpose, put in into the narrowest namespace of method parameters.

And if the method is specific method, put it in the namespace where it will be used. Also it is good to mark this method as 'internal'.

No comments:

Post a Comment