Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When is a .NET namespace implemented by a .NET Framework component?

(Yet another question from my "Clearly I'm the only idiot out here" series.)

When I need to use a class from the .NET Framework, I dutifully look up the documentation to determine the corresponding namespace and then add a "using" directive to my source code:

using System.Text.RegularExpressions;

Usually I'm good to go at this point, but sometimes Intellisense doesn't recognize the new class and the project won't build. A quick check in the Object Browser confirms that I have the right namespace. Frustration ensues.

Using HttpUtility.UrlEncode() involved adding the appropriate directive:

using System.Web;

But it also required adding a reference to .NET Framework Component for System.Web, i.e. right-click the project in Solution Explorer, select Add Reference and add System.Web from the .NET tab.

How might I discern from the documentation whether a .NET namespace is implemented by a .NET Framework Component that must be referenced? I'd rather not hunt through the available components every time I use a namespace on the off chance that a reference is needed.

(For those who like to stay after class and clean the erasers: Will Organize Usings > Remove and Sort also remove references to componenents that are not used elsewhere in the project? How do you clean up unnecessary references?)

like image 466
HABO Avatar asked Jul 26 '11 18:07

HABO


People also ask

What is namespace in .NET framework?

The System namespace is the root namespace for fundamental types in . NET. This namespace includes classes that represent the base data types used by all applications, for example, Object (the root of the inheritance hierarchy), Byte, Char, Array, Int32, and String.

What is namespace and how is it used for accessing the data in VB net?

Namespaces organize the objects defined in an assembly. Assemblies can contain multiple namespaces, which can in turn contain other namespaces. Namespaces prevent ambiguity and simplify references when using large groups of objects such as class libraries.

What is namespace in Visual Studio?

Visual Studio assigns your project name as the default root namespace for all code in your project. For example, if your project is named Payroll , its programming elements belong to namespace Payroll . If you declare Namespace funding , the full name of that namespace is Payroll. funding .


2 Answers

You'll note that the documentation (e.g. http://msdn.microsoft.com/en-us/library/system.web.httputility.aspx) tells you the name of the assembly/DLL that the class should be found in, along with the class's namespace.

Namespace: System.Web
Assembly: System.Web (in System.Web.dll)

On a side note, I know it can be a little dear, but Resharper makes things like this so much easier. If you're a serious developer, you may want to consider investing in a license. For the eraser-cleaners, Resharper adds a handy little "Find Code Dependent on Module" item to the right-click menu on references in the Solution Explorer. It's not quite an automatic cleanup, but it makes it a lot easier to see whether something's still being used by your project.

like image 36
StriplingWarrior Avatar answered Nov 10 '22 01:11

StriplingWarrior


Check out this link for UrlEncode:

Namespace: System.Web

Assembly: System.Web (in System.Web.dll)

The Assembly line tells you which dll to reference.

like image 185
dlev Avatar answered Nov 10 '22 00:11

dlev