Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

When I'm working on a project in XNA I copy the using statements from file to file. However, on tutorials I've seen for various games, they show files that only have specific using statements used. Do the other statements cause problems in the event the program doesn't need it?

I want to avoid any future problems.

share|improve this question
1  
They do that for readability. If you a source file references only the libraries it's actually using, it helps a reader understand the purpose of the code file (hint: in a month, you'll have half-forgotten this code file and you will be a reader too). For instance, if I see a sprite class that is using Microsoft.XNA.Framework.Graphics I can safely assume it handles some sprite graphics and could possibly do some drawing. [continued] –  Jonathan Hobbs Oct 5 '11 at 0:01
1  
When you begin referencing libraries you don't use, the usefulness offered by checking the libraries you're using just packs up and walks out the door. If I see your sprite class (which should probably only be doing graphics) is also using Microsoft.XNA.Framework.Audio I'll be a bit confused. I might be about to read some bad code, or I can no longer rely on the using statements to tell me what goes on in this code file. It might not even handle graphics. I just don't know anymore. This once useful readability tool now cannot be used. –  Jonathan Hobbs Oct 5 '11 at 0:03
1  
This seems off topic to me. It isn't about game development, and is more suited for stackoverflow. From the FAQ general programming questions more likely belong on Stack Overflow instead of here –  Joe Oct 5 '11 at 2:21
add comment

2 Answers

up vote 8 down vote accepted

Extra using statements will very rarely cause problems, and when they do they will be because you have introduced an ambiguous name into the scope (for example, using two namespaces with both define a Vector type, and then trying to write Vector myVector = new Vector() will be ambiguous). The compiler will tell you of these name clashes, and fail to compile your code.

If you run afoul of this, you can either fully-qualify the name reference (MyNamespace.Vector verus SomeOtherNamespace.Vector) or remove the unused using directives.

Note that if you are using Visual Studio, there is an option in the right-click menu while in the text editor for a source file to "remove unused usings." You can also have them sorted via a similar menu command.

share|improve this answer
add comment

is not used to cause problems.

the greatest problem is class or struct name collision.

if you have this:

 using System.Drawing;
 usign Microsoft.Xna.Framework.Graphics;

these namespaces both contains a Color struct, and you will have to use doing:

 System.Drawing.Color Foreground;

 Microsoft.Xna.Framework.Graphics XnaColor = Microsoft.Xna.Framework.Graphics.Color.Red;

anyway you can delete unused using statements clicking with right mouse button over the using states and selecting the appropiate option.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.