Hi Guys
NP69 “using” statements
There are many “using” statements in C#. Some of them are below. If we use using System; in a program we don’t need to use System in System.Console.WriteLine() method.
When developing a program how can one find out which code needs particular “using” statement. Anyone knows please explain.
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Text;
Thank you
Posted Dec 12, 2007, 1:15 PM
Thank you, Alan.
Thank you, Brandon Lewis.
AlanPosted Dec 12, 2007, 10:37 AM
If you start from here and choose the appropriate version:
http://msdn2.microsoft.com/en-us/library/d11h6832(VS.80).aspx
you'll see there are links to every namespace in that particular version of the .NET framework.
These links in turn list every type (class, struct, interface, enum, delegate) in the namespace and clicking on the types shows every accessible member of that type.
Notice that the info for each type always shows the namespace and also the dll that it's in. Some namespaces are split over more than one dll and, of course, a dll may contain several namespaces.
If you want to use a class but have no idea which namespace it's in, then the easiest way to find it is usually through Google. For example, if you wanted to see info on the Console class, type: 'msdn2 Console class' into the box and you should see a link direct to it in the first few entries that are returned.
Posted Dec 12, 2007, 9:34 AM
Please give me relevant online website addresses.
AlanPosted Dec 11, 2007, 6:03 PM
Whenever you use a class or other entity from the .NET Framework, you need to know what namespace it's included in. You can then check that you have a 'using' directive for that namespace to avoid having to fully qualify the class name wherever it's used.
There are, of course, a number of ways of finding out which namespace a class belongs to including the MSDN online docs, the VS docs and the object browser.
Sometimes the dll which includes the namespace has not been included in your project by default and you then need to add a reference to it, as Brandon said in his post.
Posted Dec 11, 2007, 5:56 PM
public class Values
{
public string[] GetValues()
{
//Keep in mind that none of these variables
//exist in any default namespace.
//Theyre just for example.
//I am "using" the MyFile instance
//file for the operation in the brackets.
//After that, file is disposed of.
using (MyFile file = new MyFile())
{
file.Open();
return file.ReadToEnd();
}
//MyFile is now out of scope and will be
//disposed of by the Garbage Collector
}
}
Namespaces work much in the same way, your just telling the application to use the specified namespace (or namespaces). You cant really tell which namespaces need to be included until you try to compile and your missing types or method references (Visual Studio will throw an error and ask you if you may be missing a namespace or assembly reference, the helpful soul it is). Or if you just know from experience.
If your using Visual Studio, you can look in the Solution Explorer in the right pane (Im assuming its there by default) and you will see your current file under the solution branch. If you expand the branch for it, you will see a branch called "References". This is where all of your namespace references are stored. Many of them are put there by default such as System and System.Data. If you need to add a reference in order to use its namespaces members, you can right click the references folder and click "Add Reference".
This will bring up a window where you can choose one of the many (and I mean MANY) references to add to your solution and then you can access its members. However, you have to know which reference your looking for. There are many there, such as references for Windows forms and references for various Database operations as well as references to COM interop for creating shortcuts and the like.
I suppose I didnt answer your question fully, but I hope my information was useful to you and gave you a better idea of what the using statements are all about.