How to group extension methods?

I have a static class with a set of extension methods for different types. Is there any utility or way to split it into several classes - a separate class for each target type.

+5
source share
2 answers

Using different extension methods in different classes is a good idea from the point of view of "clean code", but the main "grouping" of extension methods is by putting them in different namespaces. The reason is that extension methods are made available by "using" the appropriate namespace.

Putting different groups of extension methods in different namespaces is a good idea, as you may run into extension methods. If this happens, and each logical group of extension methods is in a fine-grained namespace, you should be able to resolve the conflict by simply deleting one of the statements using, thereby leaving the statement usingcontaining the extension method you really want.

Here is a link to some recommendations:

http://blogs.msdn.com/b/vbteam/archive/2007/03/10/extension-methods-best-practices-extension-methods-part-6.aspx

+5
source

I have another way of grouping:

public class StringComplexManager
{
    public StringComplexManager(String value)
    {
        Value = value;
    }

    public String Value { get; set; }
}

public static class StringComplexExtensions
{
    public static StringComplexManager ComplexOperations(this String value)
    {
        return new StringComplexManager(value);
    }

    public static int GetDoubleLength(this StringComplexManager stringComplexManager)
    {
         return stringComplexManager.Value.Length * 2;
    }
}

Using:

string a = "Hello"
a.ComplexOperations().GetDoubleLength()

ComplexOperation() intellisense, , , intellisense.

0

All Articles