codescracker


c#

C# Namespace



« Previous Tutorial Next Tutorial »


A namespace is used to keep one set of names separate from another.

Defining a Namespace in C#

You can define a namespace simply by using the keyword namespace followed by the namespace-name. Here is the general form to define namespace in C#:

namespace namespace-name
{
	// code
}

And to call the namespace-enabled version of either a function or variable, then just prepend the namespace name like this:

namespace-name.item-name;

C# Namespace Name

Here is an example program, demonstrates namespace in C#:

/* C# Namespace - Example Program */
		
using System;
namespace FirstNamespace
{
   class NamespaceClass
   {
      public void fun()
      {
         Console.WriteLine("Inside FirstNamespace");
      }
   }
}

namespace SecondNamespace
{
   class NamespaceClass
   {
      public void fun()
      {
         Console.WriteLine("Inside SecondNamespace");
      }
   }
}

class TestClass
{
   static void Main(string[] args)
   {
      FirstNamespace.NamespaceClass fc = new FirstNamespace.NamespaceClass();
      SecondNamespace.NamespaceClass sc = new SecondNamespace.NamespaceClass();
      fc.fun();
      sc.fun();
      Console.ReadKey();
   }
}

Now when we compile and run the above program then we will get the following output:

Inside FirstNamespace
Inside SecondNamespace

« Previous Tutorial Next Tutorial »



Tools
Calculator

Quick Links
Signup - Login - Give Online Test