ASP.net c#, how do I know what to use? - c#

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.

Related

How to find which `method` is from which `namespace` in Visual Studio

I was doing a code review and I wanted to know that if there is a way to find out which all methods are being used in a program that belong to a particular namespace. For example, in the image below:
you can see the .NET Framework Class Library namespaces that are being declared in this particular scenario.
My question is: How can I find out where a particular method of a particular namespace is being used in the program. In the image below:
you can see that when I hover on ConfigurationManager, it tells me that it belongs to the System.Configuration namespace.
I wanted to know if there are way to find a list of all methods that belong to a particular namespace in a .cs file?
Thanks in advance.
If you have ReSharper, you can put the cursor on the word "using" in the using statement, and then right-click and "Find Usages". This will show all usages of methods, classes etc in the namespace, as you require. Make sure the cursor is on "using" - if it's on the namespace name itself, then it will find all of the using statements for that namespace instead.
for my experience and if i understand you correctly,
To find where a method is being used, u can right click on the method and click "Find all References" Like this
And to know what methods are available in a cs, i only know this way

How do I find which nested namespace to import in C# for the classes I need in my code?

This is one of the things I find infuriating about C#. I have thsi massive library im trying to use right now and for some reason the people who create the code examples are not smart enough to include the namespaces you need to import in the examples. This is usualyl the case. I find myself searching through the namespace, hundreds sometimes nested trying to figure out which ones i need to import. In java netbeans, it even tells me which packages to import because it searches for me. But in C# i always waste countless time searching through the namespace manually.
Is there any way I can get around this. Like right now im trying to find which namespace contains TwitterCredentials for Tweetinvi library.
Seriously, why do people not include the namespace in code examples. It's just stupid not to!
And why does visual studio not make suggestions like java. It's just common sense really....
You can press Ctrl + . on each word that has error or point to small blue bar under the word and click on dropdown, and use offered namespace.
I am the developer of Tweetinvi :)
ALL the classes you need in Tweetinvi are located in the Tweetinvi.Core.* namespace.
TwitterCredentials.cs
To answer your question regarding Namespaces and why I do not include them in the examples. The reason is that as mentioned by Reza Aghaei Visual Studio and Resharper allow developers to include namespaces.
Furthermore as you mentioned the library is big and therefore multiple namespaces might be required and I am not going to add all of them in each example. Otherwise the documentation would just be huge.
I hope you will like the library and please feel free to ask any question here on stackoverflow or on github.
Happy coding.
PS: By the way you can also use Github to search for filename with a 't' keystroke on the Source Code page.

How do I automatically generate documentation/comments in Monodevelop?

Monodevelop automatically generates verbose documentation for functions and classes if "///" is typed in the appropriate place, upon typing the third '/'.
I want it to go over all of my code, though - I thought there was some button somewhere, and I looked around in the drop-down menus, tried right-clicking file names, etc.
I could not find such an option - does it not exist? I could not find a plugin that does that either.
Try to use Edit-> Document buffer.
It will make /// comments for all your public methods/classes without any comment upon it in current file.
It won't work if you have // comment upon method/class or for protected/private elements.
It is not possible. Your options are:
Manually typing three slashes before each and every function
Using an external tool
Online searches have not come up with any plugins for monodevelop that would do that, and the option does not exist in the vanilla IDE.

C# namespace search

I'm new to c# and I'm always Googling to find namespaces. Is there a better way, a namespace search page or something like that?
Thanks
Are you looking for the .NET Class Library: http://msdn.microsoft.com/en-us/library/ms229335.aspx
If you're using visual studio (express) you can simply use the help (either locally, or it will redirect you to the msdn website).
On the other hand I've often found that if a site offers exactly the information you look for, it's often easier to use google than the sites build in search functions :-)
There's a nice list of all namespaces in the .NET libraries on MSDN.
You could try the Resharper plugin
If you're a student or developing open source projects, it's free
Are you referring to finding the right DLL for a given namespace? Some namespaces are spread amongst several DLLs. Each class page on MSDN will list which DLL that class is in. E.G. The DataContractAttribute class is in the System.Runtime.Serialization.dll.

Enum Intellisense Display Attribute?

I want to do this:
enum Foo
{
[Display="Item One"]
ItemOne,
}
So that Intellisense will display it like in the attribute instead of the actual name.
I know it's possible, I've seen it before.
Well you could provide XML documentation:
enum Foo
{
/// <summary>Item One</summary>
ItemOne
}
I'm not sure whether that's quite what you were thinking of, but here's an example of what it looks like in VS 2010:
Note that I'm assuming you mean from the code editor... if you mean within a property editor, that could be something entirely different, e.g. DisplayNameAttribute (although that's meant for properties, events or methods).
If you know an example of what you want within the framework, we may be able to help more.
As a note... if you are building a .dll that is to be referenced by another application, just writing a summary will not allow the text to show up in intellisense for the referencing application. To accomplish this, you must deploy the XML documentation file as well, which requires a re-compiled version of the same .dll.
To do this (in VS2008 anyways), go into the Properties of your project, click the Build tab, click the checkbox at the bottom next to 'XML documentation file:', rebuild the application, and now you have the files needed to make it work.

Categories