top

Search

C# Tutorial

Namespaces are used in C# to keep one set of names separated from another. This is done to organize the classes so that they are easy to handle. If there are two classes with the same names in different namespaces, they do not conflict with one another.The syntax for namespace definition is given as follows:namespace NamespaceName {    // code }Here,        “NamespaceName” is the name of the namespace.Namespaces are further explained using the following example:Source Code: Program to implement namespaces in C#using System; namespace FirstNamespace { public class ClassOne {  public void func1() {   Console.WriteLine("This is First Namespace");  } } } namespace SecondNamespace { public class ClassTwo {  public void func2() {   Console.WriteLine("This is Second Namespace");  } } } public class NamespaceDemo { public static void Main() {  Console.WriteLine("Namespaces in C#");  FirstNamespace.ClassOne var1 = new FirstNamespace.ClassOne();  SecondNamespace.ClassTwo var2 = new SecondNamespace.ClassTwo();  var1.func1();  var2.func2(); } }The output of the above program is:Namespaces in C# This is First Namespace This is Second NamespaceNested NamespacesThere is the provision for nested namespaces in C#. This means that one namespace can be defined within another.The syntax for nested namespac is as follows:// outer namespace namespace Namespace1 { // Code // inner namespace namespace Namespace2 {  // Code } }A program that further demonstrates nested namespaces is as follows:Source Code: Program to implement namespaces in C#using System; using outernamespace; using outernamespace.innernamespace; // This is the outer namespace namespace outernamespace {                         public class outerclass {      public void func1() {         Console.WriteLine("Outer namespace");      }   } // This is the inner namespace namespace innernamespace {     public class innerclass {         public void func2() {            Console.WriteLine("Inner namespace");         }      }   } } public class Demo {   public static void Main(string[] args) {      Console.WriteLine("Nested namespaces in C#");      // Object for outer namespace         outerclass var1 = new outerclass();      // Object for inner namespace         innerclass var2 = new innerclass();      // calling functions      var1.func1();      var2.func2();   } }The output of the above program is as follows:Nested namespaces in C# Outer namespace Inner namespaceNamespace in C# vs. Packages in JavaAs an alternative of Packages in Java, the C# language has namespace.The main usage of packages in Java is to control access, to prevent naming conflicts and also to make searching and usage of interfaces, classes, annotations and enumerations easier.Packages can be defined using the keyword package followed by the package name.package packagename;Here, “packagename” is the name of the packageYou have to use import statement to mention the specific type of package:package p1.p2A namespace provides a way to keep one set of names separate from another. If there are two classes with the same names in different namespaces, they do not conflict with one another.A namespace definition begins with the keyword namespace followed by the namespace name:namespace MyNamespace;Here, “MyNamespace” is the name of the namespace.Package cannot be nested, but we can easily create nested namespace. One source file in Java can only have one package statement.We can define an alias for the namespace in C# whereas this is not possible in packages.
logo

C# Tutorial

Namespaces in C#

Namespaces are used in C# to keep one set of names separated from another. This is done to organize the classes so that they are easy to handle. If there are two classes with the same names in different namespaces, they do not conflict with one another.

The syntax for namespace definition is given as follows:

namespace NamespaceName
{
   // code
}

Here,
        “NamespaceName” is the name of the namespace.

Namespaces are further explained using the following example:

Source Code: Program to implement namespaces in C#

using System;
namespace FirstNamespace {
public class ClassOne {
 public void func1() {
  Console.WriteLine("This is First Namespace");
 }
}
}
namespace SecondNamespace {
public class ClassTwo {
 public void func2() {
  Console.WriteLine("This is Second Namespace");
 }
}
}
public class NamespaceDemo {
public static void Main() {
 Console.WriteLine("Namespaces in C#");
 FirstNamespace.ClassOne var1 = new FirstNamespace.ClassOne();
 SecondNamespace.ClassTwo var2 = new SecondNamespace.ClassTwo();
 var1.func1();
 var2.func2();
}
}

The output of the above program is:

Namespaces in C#
This is First Namespace
This is Second Namespace

Nested Namespaces

There is the provision for nested namespaces in C#. This means that one namespace can be defined within another.

The syntax for nested namespac is as follows:

// outer namespace
namespace Namespace1 {
// Code
// inner namespace
namespace Namespace2 {
 // Code
}
}

A program that further demonstrates nested namespaces is as follows:

Source Code: Program to implement namespaces in C#

using System;
using outernamespace;
using outernamespace.innernamespace;
// This is the outer namespace
namespace outernamespace {                      
  public class outerclass {
     public void func1() {
        Console.WriteLine("Outer namespace");
     }
  }
// This is the inner namespace
namespace innernamespace {
    public class innerclass {
        public void func2() {
           Console.WriteLine("Inner namespace");
        }
     }
  }
}
public class Demo {
  public static void Main(string[] args) {
     Console.WriteLine("Nested namespaces in C#");
     // Object for outer namespace   
     outerclass var1 = new outerclass();
     // Object for inner namespace   
     innerclass var2 = new innerclass();
     // calling functions
     var1.func1();
     var2.func2();
  }
}

The output of the above program is as follows:

Nested namespaces in C#
Outer namespace
Inner namespace

Namespace in C# vs. Packages in Java

As an alternative of Packages in Java, the C# language has namespace.

The main usage of packages in Java is to control access, to prevent naming conflicts and also to make searching and usage of interfaces, classes, annotations and enumerations easier.

Packages can be defined using the keyword package followed by the package name.

package packagename;

Here, “packagename” is the name of the package
You have to use import statement to mention the specific type of package:

package p1.p2

A namespace provides a way to keep one set of names separate from another. If there are two classes with the same names in different namespaces, they do not conflict with one another.

A namespace definition begins with the keyword namespace followed by the namespace name:

namespace MyNamespace;

Here, “MyNamespace” is the name of the namespace.

Package cannot be nested, but we can easily create nested namespace. One source file in Java can only have one package statement.

We can define an alias for the namespace in C# whereas this is not possible in packages.

Leave a Reply

Your email address will not be published. Required fields are marked *

Suggested Tutorials

Swift Tutorial

Introduction to Swift Tutorial
Swift Tutorial

Introduction to Swift Tutorial

Read More

R Programming Tutorial

R Programming

Python Tutorial

Python Tutorial