@Blog.Author(Nandip Makwana) .LearningExperience(ASP.NET, ASP.NET MVC, IIS, jQuery & Technology Surrounding it...)

May 20, 2009 comment ,

What is Namespaces in .NET Framework?

There are almost 13,000 classes in the .NET Framework. This is an overwhelming number. If Microsoft simply jumbled all the classes together, then you would never find anything. Fortunately, Microsoft divided the classes for similar tasks into separate namespaces
.
A namespace is simply a category. For example, all the classes related to working with the file system are located in the System.IO namespace. All the classes for working with a Microsoft SQL Server database are located in the System.Data.SqlClient namespace
.
Before you can use a class in a page, you must indicate the namespace associated with the class. There are multiple ways of doing this
.
First, you can fully qualify a class name with its namespace. For example,
.
System.IO.File.Exists ("SomeFile.txt")
.
Because the File class is contained in the System.IO namespace, you can use the above statement to check whether a file exists or not.
.
Specifying a namespace each and every time you use a class can become tedious (it involves a lot of typing). A second option is to import a namespace. You can add an <%@ Import %> directive to an ASP.NET page to import a particular namespace.
.
The following directive near the very top of the ASP.NET page imports the System.Net.Mail namespace.
.
<%@ Import Namespace="System.Net.Mail" %>
.
After you import a particular namespace, you can use all the classes in that namespace without qualifying the class names.
.
Finally, if you discover that you are using a namespace in multiple pages in your ASP.NET application, then you can configure all the pages in your application to recognize the namespace.
If you add the following web configuration file in your ASP.NET application, then you can use the classes from the System.Net.Mail namespace in each pages without adding <%@ Import %> directive to each pages.
.
<Configuration>
<system.web>
<Pages>
<Namespaces>
<Add namespace="System.Net.Mail"/>
</namespaces>
</pages>
</system.web>
</configuration>

comments powered by Disqus

Featured Content

Resources & Tools

About Nandip Makwana

Nandip Makwana is passionate about digital world and web. He completed his Masters in Computer Application in June 2011. Currently he is working as a Software Engineer. He has shown great promise and command over ASP.NET and technologies surrounding it during his academic years and professorial life...continue reading