开发者

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:

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. 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+. To get the option to automatically add the using statement (if the assembly is referenced)
  • 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)
  • Go to MSDN and search the name.
  • Go to Google and search the name (honestly, I normally do this before hitting MSDN anyway)
  • 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. 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.


If you are viewing code in Visual studio, just hover mouse over class or object you want and you will get tool tip about it if assemly of that class is present or you can google for particular class.For example if you want to know more about 'DataTable'class, just google it and you will come to know that its part of Syste.Data namespace.


I'm with the OP on this one. 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. Ctrl-Shift-I, and it will insert all the packages (ie, namespaces) you need. 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.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜