In c# we use namespace keyword to declare a logical scope. You can create various programming elements (i.e. class, method, etc. ) inside a particular namespace and these elements will be unique within that namespace.
You can refer to these elements globally from anywhere inside your project or solution by referring to the namespace they belong. This implies that the namespace provide a way to use elements in a globally unique manner.
Now let's see what type of elements (i.e. types) you can create within a namespace.
1. class
2. interface
3. struct
4. enum
5. delegate
6. another namespace
namespace TestNamespace
{
class TestClass { }
interface TestInterface { }
struct TestStruct { }
enum TestEnum { }
delegate void TestDelegate();
namespace TestNamespace.NestedNamespace { }
}
Also In the same project you can have another namespace with the same types as follows.
namespace TestNamespace
{
class TestClass { }
interface TestInterface { }
struct TestStruct { }
enum TestEnum { }
delegate void TestDelegate();
namespace TestNamespace.NestedNamespace { }
}
Also note that we can refer any of these types uniquely as follows.
namespace TestNamespaceA
{
public class TestClassA
{
TestClassA()
{
new TestClassA(); /* line1 */
new TestNamespaceA.TestClassA(); /* line2 */
new TestNamespaceB.TestClassB(); /* line3 */
}
}
}
namespace TestNamespaceB
{
class TestClassB
{
}
}
Here line1 and line2 serve the same end.
Further we can omit the namespace and create types. Then these types will fall into an unnamed namespace sometimes known as default or global namespace. This is known as global namespace because the types in this namespace can be accessed from any namespace in the project.
namespace TestNamespaceA
{
public class TestClassA
{
TestClass()
{
new ClassInGlobalNamespace(); /* line1 */
new TestClassB(); /* line2 */
new TestNamespaceB.TestClassB(); /* line3 */
}
}
}
namespace TestNamespaceB
{
class TestClassB
{
}
}
class ClassInGlobalNamespace
{
}
Note that line1 and line3 will compile but not line2. This implies that a type in ClassInGlobalNamespace can be accessed from any named namespace without specifying a reference to the ClassInGlobalNamespace namespace. Actually we don't have a so called reference to global namespace anyway.
Two other important things are,
namespace TestNamespace
{
public class TestClassA { }
}
namespace TestNamespace
{
public class TestClassB { }
}
namespace TestNamespace
{
class TestClass { }
interface TestInterface { }
struct TestStruct { }
enum TestEnum { }
delegate void TestDelegate();
namespace TestNamespace.NestedNamespace { }
}
Also In the same project you can have another namespace with the same types as follows.
namespace TestNamespace
{
class TestClass { }
interface TestInterface { }
struct TestStruct { }
enum TestEnum { }
delegate void TestDelegate();
namespace TestNamespace.NestedNamespace { }
}
Also note that we can refer any of these types uniquely as follows.
namespace TestNamespaceA
{
public class TestClassA
{
TestClassA()
{
new TestClassA(); /* line1 */
new TestNamespaceA.TestClassA(); /* line2 */
new TestNamespaceB.TestClassB(); /* line3 */
}
}
}
namespace TestNamespaceB
{
class TestClassB
{
}
}
Here line1 and line2 serve the same end.
Further we can omit the namespace and create types. Then these types will fall into an unnamed namespace sometimes known as default or global namespace. This is known as global namespace because the types in this namespace can be accessed from any namespace in the project.
namespace TestNamespaceA
{
public class TestClassA
{
TestClass()
{
new ClassInGlobalNamespace(); /* line1 */
new TestClassB(); /* line2 */
new TestNamespaceB.TestClassB(); /* line3 */
}
}
}
namespace TestNamespaceB
{
class TestClassB
{
}
}
class ClassInGlobalNamespace
{
}
Note that line1 and line3 will compile but not line2. This implies that a type in ClassInGlobalNamespace can be accessed from any named namespace without specifying a reference to the ClassInGlobalNamespace namespace. Actually we don't have a so called reference to global namespace anyway.
Two other important things are,
- Namespaces implicitly have public access and this is not modifiable in fact you can't put any modifiers or attributes to a namespace declaration.
- It is possible to define a namespace in multiple declarations as follows.
namespace TestNamespace
{
public class TestClassA { }
}
namespace TestNamespace
{
public class TestClassB { }
}