Namespaces
Namespaces?
Namespaces?
A namespace is designed for providing a way to keep one set of names separate from another. The class names declared in one namespace does not conflict with the same class names declared in another.
Defining a Namespace
Defining a Namespace
A namespace definition begins with the keyword namespace followed by the namespace name as follows
namespace namespace_name {
// code declarations
}
Example - Without the "using" keyword
Example - Without the "using" keyword
using System;
namespace first_space {
class namespace_cl {
public void func() {
Console.WriteLine("Inside first_space");
}
}
}
namespace second_space {
class namespace_cl {
public void func() {
Console.WriteLine("Inside second_space");
}
}
}
class TestClass {
static void Main(string[] args) {
first_space.namespace_cl fc = new first_space.namespace_cl();
second_space.namespace_cl sc = new second_space.namespace_cl();
fc.func();
sc.func();
Console.ReadKey();
}
}
The "using" keyword
The "using" keyword
The using keyword states that the program is using the names in the given namespace.
For example, we are using the System namespace in our programs. The class Console is defined there. We just write -
Console.WriteLine ("Hello there");
We could have written the fully qualified name as -
System.Console.WriteLine("Hello there");
Example - With the "using" keyword
Example - With the "using" keyword
using System;
using first_space; // Importing here
using second_space; // Importing here
namespace first_space {
class abc {
public void func() {
Console.WriteLine("Inside first_space");
}
}
}
namespace second_space {
class efg {
public void func() {
Console.WriteLine("Inside second_space");
}
}
}
class TestClass {
static void Main(string[] args) {
abc fc = new abc(); // Notice we are not using the fully qualified syntax
efg sc = new efg(); // Notice we are not using the fully qualified syntax
fc.func();
sc.func();
Console.ReadKey();
}
}