开发者

Support for VB.NET's Imported Namespaces feature in C#

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 sou开发者_运维知识库rce 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


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;


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(...);


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜