views:

68

answers:

4

I am use to VB.NET. The game source code I am learning from is written in C#. I find it annoying that I have to add using System.Diagnostics to the source code in order to type Debug.WriteLine.... I checked under project properties, but I cannot find the References tab that allows me to add namespaces to Imported Namespaces. Where do I find this in C#?

Also, why can't I do this in C#? Imports System.Math

A: 

In c# you should always explicitly specify namespaces you want to use or use a full name:

System.Diagnostics.Debug.WriteLine ( ... );

Also, there is a references tab under a solution view, you have to reference desired assemblies there, because many assemblies are not referenced by default.

n535
It is such a pain-in-the-butt. It is frustrating when you cannot use intellisense to find what you need.
Fred F.
Darin Dimitrov specified in his answer how you can partially solve the problem. Unfortunately this can't be used for not-referenced assemblies.
n535
Your answer reads as if you should not make use of `using` to import namespaces, which is a little confusing to me.
0xA3
No, i in no way intended to sound like this, which part you think is confusing?
n535
+3  A: 

Place the cursor over Debug in the source code, a red squiggle appears in the right bottom corner of the word, press Shift+Alt+F10 Enter - the using is automatically added.

Also, why can't I do this in C#? Imports x = System.Math

You can: using x = System.Math;

Darin Dimitrov
I am sorry, I meant Imports = System.Math. I will change my question.
Fred F.
Shift+Alt+F10 is handy. Thank you very much.
Fred F.
A much better shortcut is CTRL+. which is easier on the fingers
SLC
+2  A: 

I don't think you can have "hidden" namespaces in C# like you can in VB.NET (not sure).

As for the second part about System.Math, you can do the following at the top of each file.

using SM = System.Math;

SM.Abs(...);
JTA
I changed my question I meant Imports System.Math
Fred F.
@Fred F. : because c# does not support the Imports keyword. Also, System.Math is a class and not a namespace.
JTA
Yes, that is what the error message states, but it does not say why, and the help message for the error does not provide any information.
Fred F.
+1  A: 

It is possible to modify Visual Studio's template for new C# classes. This is not exactly the same feature as in Visual Basic, but for any newly created class you can get the namespaces that you like.

It's a little more than just a few mouse clicks unfortunately, but you will find all the details described in Anson Horton's blog post:

Item templates - adding references by default

Note that this allows you not only to modify the default using directives but also to modify the assemblies that get referenced automatically when adding a new class.

As the blog post related to Visual Studio 2005, you probably need to adjust some paths, e.g. the class.zip file is located under C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE\ItemTemplates\CSharp\Code\1033 in Visual Studio 2008.

0xA3