[.net] using namespace vs using class
If I got code in another .cs file is it better to put a 'using whatever' or just declare a variable using the class name? Removing the namespace name and just referencing the class name seems to work just as well as the other way. I'm not up on all the managed stuff so not sure if one way is bad or not.
The most common thing to do in C# seems to be just sticking a 'using' in front of every conceivable namespace. Whether this is actually the right thing to do is tougher to answer, but it's been done in every C# sample I can remember seeing. So you'll probably confuse a lot of programmers if you do anything else... [wink]
It's just a style thing.
Generally, I will "use" the namespaces that I intend to make heavy use of in a particular .cs file. If I'm only going to reference a type from another namespace once, I'll usually just qualify it fully.
Generally, I will "use" the namespaces that I intend to make heavy use of in a particular .cs file. If I'm only going to reference a type from another namespace once, I'll usually just qualify it fully.
Namespaces are there for a reason. The reason is tohelp handling naming conflicts between different libraries, it is amazing how often they have the same class names. If a project I am working on I use both the System.Windows.Forms Namespace and all of the XNA framework, but they have classes with the same names. So becuase I am doing more with the XNA framework I use the Using statements to make the calling of XNA classes easier and us the full System.Windows.Forms.whatever naming for the Forms stuff.
What I believe is a good rule of thumb. Use the Using statement and the shorter naming convention when there will be no confusing about what the class is. Use the Full naming convention when there is any chance of confusion.
theTroll
What I believe is a good rule of thumb. Use the Using statement and the shorter naming convention when there will be no confusing about what the class is. Use the Full naming convention when there is any chance of confusion.
theTroll
Maybe I'm not doing something right, either using the namespace or the class all I do is make a new variable of the class name.
Even if I drop the namespace I still create the variable the same and access the member functions in the same way. The only thing that seems to be different is the intellisense tag when you hover over the variable.
namespace tester123 { class Tester {
Tester a = new Tester();
Even if I drop the namespace I still create the variable the same and access the member functions in the same way. The only thing that seems to be different is the intellisense tag when you hover over the variable.
Within a namespace all the classes in that namespace are visable. So you don't need the #using statement or the full naming convention.
theTroll
theTroll
Don't forget that you can alias namespaces in C#... using Alias = Type|Namespace;. This can be useful in keeping name scoping in place, while at the same time decreasing the length of fully qualified names.
Quote:Original post by TheTroll
Within a namespace all the classes in that namespace are visable. So you don't need the #using statement or the full naming convention.
theTroll
This I don't quite get. In the Tester class I have a function named xx(). Either way I try it I still have to use a.xx() or it says it doesn't exist in the current context.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement