Introduction
Namespaces are basic building block of .NET Framework based programming.
Namespace helps the programmer to define the class and class has a big list of
properties, events and methods. The picture given below will outline some
concepts.
As we all know, there are over 13,000 classes included in the .NET Framework
2.0. This is really very big list of number. Think if Microsoft bundled all the
classes together, then we have to loss our lots of time to find any event,
properties or else. So, Microsoft divided the .NET Framework into Assemblies,
Classes and Namespaces. Before we can use a class in a page, we must indicate
the namespace associated with the class. There are multiple ways of doing this.
- We can fully qualify a class name with
its namespace. For example, because the File class is contained in the
System.IO namespace, we can use the following statement to check whether a
file exists:
System.IO.File.Exists("introduction.txt")
Specifying a namespace each and every time is
very difficult and also it involves a lot of typing work. So we can use the
following one.
- We can add an <%@ Import %> directive to
a page to import a particular namespace. System.Net.Mail namespace we use
for mailing and SmtpClient is part of this namespace.
<%@
Import
Namespace="System.Net.Mail"
%>
- Above one is very cool technique and used
in maximum application but if we have to use System.Net.Mail Namespace
many-times in same application then we can use that namespace inside the
configuration file as given below. We can add a web configuration file to
our application by selecting Website, Add New Item and selecting Web
Configuration File. We can add multiple Configuration file folder wise for
separate purpose. If we add namespace in root Configuration file then it
will affect entire web project.
<?xml
version="1.0"?>
<configuration>
<system.web>
<pages>
<namespaces>
<add
namespace="System.Net.Mail"/>
</namespaces>
</pages>
</system.web>
</configuration>
Once if we add the web configuration file to
our application, then we do not need to import the System.Net.Mail namespace in
a page to use the classes from this namespace.
HAVE A GREAT CODING!