简体   繁体   English

ASP.net C#,我怎么知道使用什么?

[英]ASP.net c#, how do I know what to use?

I keep coming accross code samples online for ASP.net c#, however they never seem to list which namespaces they include, for example: 我不断在网上看到有关ASP.net c#的代码示例,但是它们似乎从未列出它们包括哪些名称空间,例如:

using System.Data.SqlClient;
etc etc

Am I missing something obvious or should I be expected to know exactly what namespaces each code example requires? 我是否遗漏了明显的东西,还是应该确切地知道每个代码示例所需的命名空间?

When I'm in that situation, typically I search for the class on MSDN . 在这种情况下,通常我会在MSDN上搜索该类。 The documentation will tell you which namespaces contain the class. 该文档将告诉您哪些名称空间包含该类。

If they don't include them, you can follow this list in order: 如果它们不包括它们,则可以按顺序关注此列表:

  • Find that they are in one of the namespaces listed in the "blank code file" template , or 查找它们是否在“空白代码文件”模板中列出的名称空间之一中,或者
  • In Visual Studio You can click the missing type and press shift+F10 or Ctrl+. 在Visual Studio中,您可以单击缺少的类型,然后按shift + F10或Ctrl +。 To get the option to automatically add the using statement (if the assembly is referenced) 获取自动添加using语句的选项(如果引用了程序集)
  • With Resharper , Select the type and hit alt+enter for Resharper to find the namespace for you, and add it to the usings (possibly even reference the assembly as well) 使用Resharper ,选择类型并单击Resharper的alt + enter来找到适合您的名称空间,并将其添加到使用中(甚至还可以引用程序集)
  • Go to MSDN and search the name. 转到MSDN并搜索名称。
  • Go to Google and search the name (honestly, I normally do this before hitting MSDN anyway) 转到Google并搜索名称(老实说,我通常会在访问MSDN之前这样做)
  • Compain to the article author 与文章作者比较

If code samples use the assemblies that a project references by default, then you can hover on the class name and click shift+F10 which will add the using statement automatically. 如果代码示例使用默认情况下项目引用的程序集,则可以将鼠标悬停在类名上,然后单击shift + F10,这将自动添加using语句。 If the class is not in any of the referenced assemblies then you are out of luck and need to know in what assembly does the class resides. 如果该类不在任何引用的程序集中,那么您很不幸,需要知道该类驻留在哪个程序集中。

A quick google search can help, and in time you will memorize the namespaces... Of course its best if samples included the namespace and reference info, but mostly they do not. 快速的Google搜索会有所帮助,并且您会及时记住名称空间...当然,如果示例中包含名称空间和参考信息,那将是最好的选择,但是大多数情况下却没有。

如果您正在Visual Studio中查看代码,只需将鼠标悬停在所需的类或对象上,如果存在该类或可以谷歌搜索特定的类,例如,如果您想了解更多有关该类的信息,则将获得有关该类的工具提示。 “ DataTable”类,只需在Google上搜索它,您就会知道它是Syste.Data命名空间的一部分。

I'm with the OP on this one. 我在这上面有OP。 Having to just magically "know" what namespaces are required seems supremely silly. 不得不神奇地“知道”需要什么名称空间似乎非常愚蠢。

I spent some time before C# as a Java Developer, and the NetBeans IDE will resolve these for you automatically. 我花了一些时间在C#担任Java开发人员之前,NetBeans IDE会自动为您解决这些问题。 Ctrl-Shift-I, and it will insert all the packages (ie, namespaces) you need. Ctrl-Shift-I,它将插入您需要的所有包(即名称空间)。 If more than one package defines the class you are resolving, a window pops up and lets you choose which one you want. 如果一个以上的软件包定义了您要解析的类,则会弹出一个窗口,让您选择所需的类。

For as fine a product as VS is, I am incredulous that this feature is not included. 对于像VS这样出色的产品,我非常怀疑不包含此功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM