YouTip LogoYouTip

Csharp Namespace

# C# Namespace The purpose of a **namespace** is to provide a way to separate one set of names from another. A class declared in one namespace will not conflict with the same class name declared in another namespace. Let's take an example from a computer system: a folder (directory) can contain multiple folders. Each folder cannot have files with the same name, but files in different folders can have the same name. !(#) ## Defining a Namespace A namespace is defined using the keyword **namespace**, followed by the namespace name, as shown below: namespace namespace_name { // code declarations } To call a function or variable that supports a namespace version, you place the namespace name in front, like this: namespace_name.item_name; The following program demonstrates the use of namespaces: ## Example 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(); } } When the above code is compiled and executed, it produces the following result: Inside first_space Inside second_space ## The _using_ Keyword The **using** keyword indicates that the program is using the names in the given namespace. For example, we are using the **System** namespace in our program, where the class Console is defined. We can simply write: Console.WriteLine ("Hello there"); We can write the fully qualified name, as follows: System.Console.WriteLine("Hello there"); You can also use the **using** namespace directive, so that you don't have to qualify the name of an item that is declared in that namespace. This directive tells the compiler that the subsequent code is using the names in the specified namespace. The following code demonstrates the use of namespaces. Let's rewrite the above example using the using directive: ## Example using System; using first_space; using second_space; 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(); efg sc = new efg(); fc.func(); sc.func(); Console.ReadKey(); } } When the above code is compiled and executed, it produces the following result: Inside first_space Inside second_space ## Nested Namespaces Namespaces can be nested; that is, you can define one namespace within another namespace, as follows: namespace namespace_name1 { // code declarations namespace namespace_name2 { // code declarations } } You can access members of a nested namespace using the dot (.) operator, as shown below: ## Example using System; using SomeNameSpace; using SomeNameSpace.Nested; namespace SomeNameSpace { public class MyClass { static void Main() { Console.WriteLine("In SomeNameSpace"); Nested.NestedNameSpaceClass.SayHello(); } } // nested namespace namespace Nested { public class NestedNameSpaceClass { public static void SayHello() { Console.WriteLine("In Nested"); } } } } When the above code is compiled and executed, it produces the following result: In SomeNameSpace In Nested [](#)[C# Interface](#) [C# Preprocessor Directives](#)[](#)
← Dom Obj CanvasCsharp Interface β†’