Jump to content
  • Advertisement
Sign in to follow this  
darkchrono4

[.net] using namespace vs using class

This topic is 4220 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
Advertisement
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]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.


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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!