Wednesday, December 07, 2005

C# Self Tutor (Chapter 4)


NAMESPACES AND USING STATEMENTS:

Definition:
namespace:
Namespace can be defined as the logical collection of classes and their related functions. Using directive is used to facilitate the use of namespaces. A single C# file can declare several namespaces. The namespaces are implicitly public. The system namespace is the root namespace for fundamental types in the .NET framework. The system namespace also contains many second-level namespaces. Within a namespace one can declare another namespace, class, interface, struct, enum, and delegate.
USING DIRECTIVE:
Using directives enable the use of namespace and types defined in the namespaces. The types within the namespace can be referred by the fully qualified name.
Using system
Namespace n1
{
Public class ebony1
{
Public static void main ()
{
N2.ebony2.diaplay ();
}
}
Namespace n2
{
Public class ebony2
{
Public static void display ()
{console.writeline ("sky is not ur limit; u have to go beyond sky.");
}
}
}
}
Output:
sky is not ur limit; u have to go beyond sky
.
The n2 namespace has the class ebony2. The display function of n2 namespace has been invoked from namespace n1 by declaration n2.ebony2.diaplay ().





NAMESPACE ALIAS:
Namespace alias declaration enables a user-defined symbol to represent a namespace. Then one can be able to represent the namespace name.
Using system;
Using np=n1.n2.ebony2;
Namespace n1
{
Public class ebony1
{
Public static void main ()
{
Np.display ();
}
}
Namespace n2
{
Public class ebony2
{
Public static void display ()
{
Console.writeline ("sky is not ur limit; u have to go beyond sky");
}
}
}
}
Output:
sky is not ur limit; u have to go beyond sky
The output of this program is same as the output of the previous program. In this program the namespace alias is defined as np for n1.n2.ebony2.

NAMESPACE HIERARCHY:
In C# one can organize the classes using namespaces. The following example demonstrates the organization of classes.
Using system;
Namespace n1
{
Class ebony1
{
Public static void main ()
{console.write ("how r u?");
Show s1=new show();
S1.display ();
}
}
}
Class show
{public void display ()
{
Console.write ("fine");
}
}
Output:
How r u? fine
Here the hello class belongs to the namespace n1. But the class show is not having any enclosing namespace and it is considered to be a member of the global namespace.
There are many pre-defined namespace like system.IO (contains classes related to input and output), system.reflection (provides the methods to describe the classes used in a program), system.data (contains the classes that constitute ADO.Net architecture), system.threading (contains classes related to threading), sytem.xml (contains classes related to XML).



0 Comments:

Post a Comment

<< Home