One feature of vb compiler is that namespace declaration behaves in an inheritance kind of pattern. It is defined in applicaiton level and code files only specify namespace if it is other than the default.
Is it possible to do the same in C#
Hi!
Perhaps I haven't made it clear. I know that it is not possible using standard compiler or VS features. I was wondering if there is any kind of hack to do this.
Beside the compiler and VS, what else is there
No, each CS file must delcare or import namespaces.
Well, I suppose there could be a "hack" (ugly, as all hacks would be). You could store the namespace in a property somewhere, and have each source file not declare their namespace; and introduce and use your own 'pre-compiler' to copy each source file to a temporary file, inject the namespace block into it, feed it to the underlying compiler, and delete the temp file. This also has the undesired effect of making any symbolic debugging of no use as not only do the source file names not match up in the assembly, their line numbers do not either.
Anyway, bottom line:
In the end, what the compiler operates on must have the namespace block present (unless you write your own custom compiler - another hack of much greater magnitude!)
You don't need a namespace block. For example, if the following were the only content in "test.cs":
CSC would compile it without error.
There is no such hack, but there is something much better. Since Visual Studio 2005 if you include some type for which the namespace is not included, then a red hint under the end of type is shown which if clicked will show you a list of posible namespace that will be included if you click on one. There is other tools that can help in this problem even better. For example ReSharper is working great with namespaces. It will mark all the namespaces that are not used, but also it will show a opened hint which with click will include the necessary namespaces. This addin goes much further then this, like remove the namaspace from fully qualified types and put the namespace at the top of file, and lot other stuff.
Visual studio 2008 will go even further, and if you start using some type from .NET framework assembly which is not even referenced in the project, it will give you a hint wich on mouse click will add reference to the project, and of course add the namespace on the top of file. There are also other features that will manage namespaces in code file.
Importing namespaces in code file have good side but also bad one. If you add to many namaspace, then using intellisense becomes harder, and also you can encounter a lot slower IDE on some less powerfull machine. So it is always best to have only namespaces that are used.
Since I don't understand the post entirely, I'll just echo what you said. Resharper is a great tool to refactor your code so that all your namespaces are at the top. And it will tell you (by greying references out) what name spaces are no longer in use.
If you simply want to get rid of the list of namespaces, hide it in a region. You could even, though this is dirty, create a partial class and put all the code in there that you don't want to see. Please don't do that ;-)
Roy
"this topic is about automatically setting the namespace of C# classes one writes,"
Why would you do that What if you aren't going to use the namespace Do you know before-hand what namespaces you will always be using
Roy, he's talking about the namespace DECLARATIONS (e.g., "namespace MyNamespace { ....class goes here ... }") not the import statements (e.g., "import MyNamespace;").
I knew it was one of the two ;-) Thanks.
Hi papadi,
After a long discussion for your concern, we have known that C# can't have the same feature as VB.NET by default when creating a new class file. If you would like to create a C# class file without namespace like VB.NET, maybe writing a custom C# class template for VS.NET by yourself is an idea to solve this issue. But this is not an easy way to go.
If you are intersted in this, you can try to check out the following articles for your reference
#1 Creating Custom Project template Wizards Using C# and .NET:
http://www.c-sharpcorner.com/UploadFile/mgold/CustomWizard11292005004112AM/CustomWizard.aspx
#2 Creating Custom Item Templates in Visual Studio 2005
http://codebetter.com/blogs/david.hayden/archive/2005/11/06/134343.aspx
Hope this helps,
Regards,
Citizens on the earth