Click here to Skip to main content
15,118,063 members
Articles / Programming Languages / C#
Technical Blog
Posted 3 Oct 2013

Tagged as

Stats

9.7K views
5 bookmarked

Extension methods are awesome, but that dang namespacing…

Rate me:
Please Sign up or sign in to vote.
2.92/5 (7 votes)
3 Oct 2013CPOL2 min read
A useful utility.

I think we can all agree that extension methods are something that many of us have taken quite a shining to. Using them to adapt external dependencies, encapsulate common functionality for a 3rd party, and other conveniences has really helped to clean up my own codebase – I know that much.

The one thing that I do find annoying, though, is the fact that in order to get an extension method to show up in the editor (and be usable, for that matter) is you have to include the namespace in which the method’s located in your ‘using’ directives of your class file. This pains me because not only do I like to keep my using directives clean (Remove and Sort on Save anybody?), but also I’m annoyed on more than just a handful of occasions when my code fails to compile, the using directive gets removed, and I have to go add it back again. Resharper helps out by automatically knowing where these are, but without it you’re kind of left typing and retyping, and expanding the Object Browser to find out what namespace the extension lives in, blah blah blah.

On a hunch I tried something out the other day, and it actually worked pretty slick, so I thought I’d pass it along.

Create your extension class in a cs file as you normally would:

C#
public static class DictionaryExtensions
{
    /// <summary>
    /// Adds or replaces the given key/value pair within the dictionary
    /// </summary>
    /// <typeparam name="TKey">Type of the keys in the dictionary</typeparam>
    /// <typeparam name="TValue">Type of the values in the dictionary</typeparam>
    /// <param name="dict">The dictionary that will have the value added or replaced</param>
    /// <param name="key">The key to add to the dictionary if it doesn't exist</param>
    /// <param name="value">The value to associated with the found/added key</param>
    public static void AddOrReplace<TKey, TValue>(this Dictionary<TKey, TValue> dict, TKey key, TValue value)
    {
        if (dict.ContainsKey(key))
        {
            dict[key] = value;
        }
        else
        {
            dict.Add(key, value);
        }
    }
}

But notice what I don’t have specified there. The namespace. what this does is makes this extension class show up in the global namespace. The effective result is the second you reference the library I’ve put this class in, you get the extensions – no matter what namespaces you have imported. So now any time I am playing w/ Dictionary<TKey, TValue> and have a reference to my Extensions libarary, I get my new AddOrReplace extension. For free. Automatically.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

BC3Tech
Software Developer (Senior)
United States United States
I'm a Sr. Software Engineer in the Seattle area primarily focused on serverless technologies in the cloud. In my free time I enjoy hiking & other adventures with my family around the Puget Sound and the country! You can find out more about me at my homepage: http://bc3.tech/brandonh

Comments and Discussions

 
GeneralMy vote of 2 Pin
Rui Jarimba10-Oct-13 3:51
professionalRui Jarimba10-Oct-13 3:51 
GeneralMy vote of 2 Pin
ke4vtw8-Oct-13 6:26
Memberke4vtw8-Oct-13 6:26 
SuggestionTip/trick Pin
Qwertie7-Oct-13 13:25
MemberQwertie7-Oct-13 13:25 
GeneralRe: Tip/trick Pin
BC3Tech7-Oct-13 15:38
MemberBC3Tech7-Oct-13 15:38 
QuestionDangerous idea Pin
Kuzhelev Roman4-Oct-13 23:46
MemberKuzhelev Roman4-Oct-13 23:46 
GeneralMy vote of 1 Pin
Athari4-Oct-13 2:57
MemberAthari4-Oct-13 2:57 
GeneralThoughts Pin
PIEBALDconsult3-Oct-13 14:41
professionalPIEBALDconsult3-Oct-13 14:41 
QuestionNot such a good idea Pin
William E. Kempf3-Oct-13 10:12
MemberWilliam E. Kempf3-Oct-13 10:12 
AnswerRe: Not such a good idea Pin
BC3Tech3-Oct-13 10:18
MemberBC3Tech3-Oct-13 10:18 
understood. any time you're creating a reusable library namespacing is important. this is a trick I have found useful in my own personal development of applications.

thanks for pointing it out, though, for readers that might not infer it Wink | ;)
GeneralRe: Not such a good idea Pin
William E. Kempf3-Oct-13 10:28
MemberWilliam E. Kempf3-Oct-13 10:28 
GeneralGood idea but ... Pin
Carlos A Alvarez3-Oct-13 6:07
MemberCarlos A Alvarez3-Oct-13 6:07 
GeneralRe: Good idea but ... Pin
BC3Tech3-Oct-13 6:34
MemberBC3Tech3-Oct-13 6:34 
GeneralRe: Good idea but ... Pin
PIEBALDconsult3-Oct-13 14:19
professionalPIEBALDconsult3-Oct-13 14:19 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.