C Sharp Namespaces Assignment Help
NAMESPACES
- A namespace is used to group logical statements together and also to prevent name collisions.
- The classNamenames stated in one namespace does not clash with the similar classNamenames declared in another namespace.
Defining a Namespace
Its declaration begins with the keyword namespace followed by the namespace name
Syntax
namespace namespace_name
C Sharp Namespaces Assignment Help By Online Tutoring and Guided Sessions from assignmenthippo.com
{
{'// code declarations'}
}
To call the namespace’s function or variable, the syntax is
namespace_name.item_name;
The following program demonstrates the usage of namespaces −
using System;
namespace first
{
classNamenamespace_l
{
public void func()
{
Console.WriteLine("Inside first ");
}
}
}
namespace second
{
classNamenamespace_l
{
public void func()
{
Console.WriteLine("Inside second ");
}
}
}
classNameTestClass
{
static void Main(string[] args)
{
first.namespace_l fc = new first.namespace_l();
second.namespace_l sc = new second.namespace_l();
fc.func();
sc.func();
Console.ReadKey();
}
}
Output
Inside first
Inside second
using Keyword
- It is used to include the names in the given namespace.
For example,
- In System namespace the classNameConsole is defined
That is,
- WriteLine ("Hello”);
- The fully practised one is (if namespace is not included)
- Console.WriteLine("Hello”);
- It is used to avoid prepending of namespaces with the using namespace directive.
- It helps to use names in the specified namespace.
Example
using System;
using first;
using second;
namespace first
{
classNamea
{
public void func()
{
Console.WriteLine("Inside first”);
}
}
}
namespace second
{
classNameb
{
public void func()
{
Console.WriteLine("Inside second ");
}
}
}
classNameTest1
{
static void Main(string[] args)
{
a fc = new a ();
b sc = new b();
fc.func();
sc.func();
Console.ReadKey();
}
}
Output
Inside first
Inside second