Creating Local Extension Methods

Sometimes, it can be useful to make an extension method specifically for a single block of code.  Unfortunately, since extension methods cannot appear in nested classes, there is no obvious way to do that.

Instead, you can create a child namespace containing the extension method.  In order to limit the extension method’s visibility to a single method, you can put that method in a separate namespace block.  This way, you can add a using statement to that namespace alone.

For example:

namespace Company.Project {
    partial class MyClass {
        ...
    }
}
namespace Company.Project {
    using MyClassExtensions;
    namespace MyClassExtensions {
        static class Extensions {
            public static string Name<T>(this T obj) {
                if (default(T) == null && Equals(obj, default(T)))
                    return "(null " + typeof(T) + ")";
                return obj.GetType() + ": " + obj.ToString() 
                     + "{declared as " + typeof(T) + "}";
            }
        }
    }
    partial class MyClass {
        void DoSomething() {
            object x = new DateTime();
            string name = x.Name();
        }
    }
}

Since the using MyClassExtensions statement appears inside the second namespace block, the extension methods are only visible within that block.  Code that uses these extension method can appear in this second block, while the rest of the class can go in the original namespace block without the extension methods.

This technique should be avoided where possible, since it leads to confusing and non-obvious code.  However, there are situations in which this can make some code much more readable.

11 comments:

I would like to read more of your posts. Very nice post thank you for sharing.
usps tracking

The above article is nice and interesting, thank you willing to share! Greetings success of admin Percetakan Murah Rawamangun Jakarta Timur wish you deign to visit my website, thank you :)

It will be great idea for college students to check https://nerdymates.com/blog/apa-style-format out. Here you can read more about APA style format

Your blog is filled with unique good articles! I was impressed how well you express your thoughts.
zarchiver for pc

Hi people! If some of you necessity to assistance guys with essay or homework - you can check ghostwriter websites and beresidency personal statement! This guys really know how to do it, and you can be the best! Excellent luck and have fun!

Great blog and you explained this method very easily I was trying to learn this method but I didn't find the right guy to explain me but I think your that who I'm looking for thanks for explaining this amazing topic to us. Nowadays logo has become an important part of every website everyone wants the best and most unique logo for their websites but not all of them can afford this service. Logo Designer PK is providing services at very cheap prices that anyone can easily afford their services. They are the best they didn't compromise with their quality of work so make sure you avail of their services.

Save with the latest UK deals including 10% OFF using our Boden Discount Code Score a minimum of 20% off selected girl clothes orders for a limited time only.

Post a Comment