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

☆樱花仙子☆ 提交于 2019-12-11 02:36:18

问题


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


回答1:


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;




回答2:


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



回答3:


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.




回答4:


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.



来源:https://stackoverflow.com/questions/2605995/support-for-vb-nets-imported-namespaces-feature-in-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!