Namespace for Extension Methods

Recently We had a discussion about what namespace would you give to an extension method. Most answers to this question say you should put it in something like “Extensions” or “YourCompany.Technology.Extensions” (like here and here).

The problem with that is probably known to any developer. Since Visual Studio 97 where IntelliSense was added, you learn about the class functionality from the Intellisense list, but the extension method won’t appear in the list until you add the extension method namespace in your using list. Sure it happened to you when you wrote something like “myControl.Nodes.” and you were surprised to see there is no “Where” method. You must have asked yourself “Did they forget to implement IEnumerable when .NET 2.0 was out?”, and after that you try to find the “OfType“ method but you don’t find it either. A quick look will show you that the “using System.Linq;” statement is missing.</p>

This namespace is not there because of possible two reasons:

  1. This file was created before .NET 3.5 where Microsoft added this namespace to any C# file template.
  2. You used the “Remove Unused Usings” feature when no IEnumerable was in use in that file. (Use CodeMaid to do that. It has a white list for that, among many other great features). BTW, unused usings don’t have any effect on run time, only design and compile time. Look here and here.</li> </ol>

    Same goes for any extension method you write. If your colleague won’t remember the method you wrote (and probably you either won’t remember it after a while), he won’t have any clue about its existence. Even if you add this namespace to any C# template in your organization (Try to change the namespace after that!), it could be deleted.

    So why won’t we go back to the days of the big Utils class?

    I think we should name extension method namespace after the first parameter (the one with “this“) type namespace. For example, if you’re extending DataObjects.Person, give your extension method the DataObjects namespace. Most times when you use a class in your code you have its namespace inside a using statement (unless you got the instance from somewhere else).

    namespace DataObjects
    {
        public static class PersonExtensions
        {
            public static string GetFullName(this Person p)
            {
                return p.FirstName + " " + p.LastName;
            }
        }
    }

    Now what if you want to extend System.String. Someone might think that writing your extension method in System namespace is not allowed / won’t compile / not possible. Well, it is possible and won’t cause you any problem (I’m not an attorney, though).

    Think of an extension method as a method you want to inject into the class being extended, so why not inject it into its namespace also?

    MSDN namespace naming guidelines say:

    You should make every effort to avoid name collisions with types in the core namespaces

    As for extension methods, this is not going to cause any collision at all. The thing your should be aware of, is that if Microsoft will ever write a method with the same name and parameters in the class you’ve extended, all your method calls will now go to their code, ignoring your extension method without any compile error or warning. However, it doesn’t matter what is the namespace you have chosen, or which using statement you added.

    You just need to find a class name and a method that won’t collide with the core types ever. “iPhoneGoogleFacebook” might be a good prefix. They are not going to buy all of them, don’t they?

Thank you for your interest!

We will contact you as soon as possible.

Send us a message

Oops, something went wrong
Please try again or contact us by email at info@tikalk.com