declaring a namespace

  • Thread starter Thread starter Michael Roper
  • Start date Start date
M

Michael Roper

If I've got something like:

using System;

namespace myCompany.myLibrary
{
...class definitions here...
}

in a file, such that there's nothing outside of the namespace block except
the using statement(s), is there an alternative way to say the same thing
that doesn't require the extra level of nesting for the namespace?

That is, I'm looking for a declaration that effectively says "everything in
this file is in the namespace myCompany.myLibrary," and eliminates the need
for the actual namespace declaration block.

Michael Roper
 
Hi,
Why don't you use the using statement ?
It's quite simple and convinient !
 
'Using' won't solve the problem. AFAIK, C# is always plagued by an extra
indentation of indent due to the namespace { }construct that you wouldn't
have in Java. I haven't heard a good explanation of why... I can't imagine
wanting to include code for two different namespaces in one file. (And in
Java, with one class per file, it would be impossible even if they used the
namespace construct like C#). So here, I have to say that I prefer the Java
way.
 
Another thought, get a wide-screen monitor so that you don't have to worry
about the extra indentation ;-)
 
Michael Mayer said:
'Using' won't solve the problem. AFAIK, C# is always plagued by an extra
indentation of indent due to the namespace { }construct that you wouldn't
have in Java. I haven't heard a good explanation of why... I can't imagine
wanting to include code for two different namespaces in one file. (And in
Java, with one class per file, it would be impossible even if they used the
namespace construct like C#). So here, I have to say that I prefer the Java
way.

Although its rare, namespaces can be nested for a purpose. I wouldn't
normally put things in multiple namespaces in a single file, but I would
also not normally split a type between files, yet we are getting partial
type support.
I would suspect nested namespace support is of use most often in files that
declare lots of little declarations. Even though I know I shouldn't, alot of
the time I define multiple delegates or highly simple exception\EventArgs
classes (in essence, do nothing but provide a new specific type), or other
really simple classes in a single file or in files adjoining the type that
will use them instead of adding dozens of 6 line files.
Also, someone might find it more readable to do
namespace Level1
{
namespace Level2
{

}
}
even if it would drive me nuts(and does in C++, as best I can tell thats the
only way to nest namespaces, yet another reason I never liked that lanuage)
 
Daniel O'Connell said:
Although its rare, namespaces can be nested for a purpose.
I agree with the idea of nesting namespaces. e.g.
System.Data
System.Data.OleDb;

I would suspect nested namespace support is of use most often in files that
declare lots of little declarations. Even though I know I shouldn't, alot of
the time I define multiple delegates or highly simple exception\EventArgs
classes (in essence, do nothing but provide a new specific type), or other
really simple classes in a single file or in files adjoining the type that
will use them instead of adding dozens of 6 line files.

Absolutely, I do the same thing when there's a very tight connection between
a small thing (like a delegate) and a bigger class that uses it. However,
if there's a farily loose coupling between the two (enough to merit that
they go in different namespaces) then I would sure think that separate files
would be desirable as well.
Also, someone might find it more readable to do
namespace Level1
{
namespace Level2
{

}
}
even if it would drive me nuts(and does in C++, as best I can tell thats the
only way to nest namespaces, yet another reason I never liked that
lanuage)

That would drive me nuts as well.
 
Back
Top