2

i wanted to know as i start to make some dramatic move from using regular helpers to more and more extension methods so they start to pile up ,

say my top main namespace is as follows :

the main namespace name is myname + last digit of year and current month

thats how i keep it organized as a helper namespace

namespace "myname212" 
{
    namespace DbRelated
    {
        some clasess & methods 
    }

    namespace styling
    {
        same as usual ..
    }

    // .... some more categories and...then

    //the extentions namespace

    namespace CustomExtentions
    {
        // simplest ext class and its first method 
        public static class ToNumber
        {
            public static int Toint(this Textbox TbxToConvrt)
            {
                return Convert.ToInt32(TbxToConvrt.Text);
            }
            //some more of same subject 
        }
    }
}

but what if i have a more general catefory that has it own sub category logocally

namespace Extentions
{
    public static class MainCategory
    {
         public static class SubCat1 
         {
             public static some_method();
         }
         public static class SubCat2 
         {
             public static some_method();
         }
    }  
}

the a hierarchical stracture above will not work .

so is it true that if i'd like to build more categories i could only do it via

nested namespaces instead of nested classes ?

is this what you do ?

abatishchev
  • 98,240
  • 88
  • 296
  • 433
LoneXcoder
  • 2,121
  • 6
  • 38
  • 76
  • Why will not work? Is isn't compilable or you just don't like/don't accept it? – abatishchev Dec 07 '12 at 01:49
  • it will compile though won't be accesed as an extension (are you missing an assembly or ....) – LoneXcoder Dec 07 '12 at 01:52
  • @abatishchev did it ever work for you , did you try ? am i missing somthing ? the folowing strucure works for you as an extension ?: `namespace-> static class-> static class--> static method();` from outside to inside as like this works for you ? – LoneXcoder Dec 07 '12 at 01:56
  • 1
    @LoneXcoder why do you need to nest static classes containing extension methods? It looks very clear to nest namespaces...even to keep your year-namings: `namespace Extensions.SomeName2012.January`, then `namespace Extensions.SomeName2012.February` and so on...so you only need to add the suitable `using` – horgh Dec 07 '12 at 02:19
  • Okay, I see now. You nest not namespaces but classes. That can not work indeed. – abatishchev Dec 07 '12 at 02:21
  • @LoneXcoder read [Why Would I Ever Need to Use C# Nested Classes](http://stackoverflow.com/questions/1083032/why-would-i-ever-need-to-use-c-sharp-nested-classes) – horgh Dec 07 '12 at 02:23
  • @KonstantinVasilcov thx, ill have a look now – LoneXcoder Dec 07 '12 at 02:25

1 Answers1

4

You can nest namespaces instead:

namespace My.Nested.Namespace.So.Far
{
    public static class BlaExtensions
    {
    }
}

or even:

namespace My.Nested.Namespace
{
    public static class FooExtensions
    {
    }

    namespace So.Far
    {
        public static class BlaExtensions
        {
        }
    }
}

should work too.

abatishchev
  • 98,240
  • 88
  • 296
  • 433