what is namespace

Posted by Jameschowdare under C# on 4/16/2009 | Views : 20872 | Status : [Member] | Replies : 2

Responses

Posted by: Vuyiswamb on: 4/16/2009 [Member] [MVP] [Administrator] NotApplicable

Up
0
Down
Good Day

A Namespace in Microsoft .Net is like containers of objects. They may contain unions, classes, structures, interfaces, enumerators and delegates. Main goal of using namespace in .Net is for creating a hierarchical organization of program. In this case a developer does not need to worry about the naming conflicts of classes, functions, variables etc., inside a project.

In Microsoft .Net, every program is created with a default namespace. This default namespace is called as global namespace. But the program itself can declare any number of namespaces, each of them with a unique name. The advantage is that every namespace can contain any number of classes, functions, variables and also namespaces etc., whose names are unique only inside the namespace. The members with the same name can be created in some other namespace without any compiler complaints from Microsoft .Net.
To declare namespace C# .Net has a reserved keyword namespace. If a new project is created in Visual Studio .NET it automatically adds some global namespaces. These namespaces can be different in different projects. But each of them should be placed under the base namespace System. The names space must be added and used through the using operator, if used in a different project.

Please now have a look at the example of declaring some namespace:

using System;

namespace OutNamespace
{

namespace WorkNamespace
{ /// can be placed some classes, structures etc.
}

}


In this example we create two namespaces. These namespaces have hierarchical structure. We have some outer one named OutNamespace and the inner one called WorkNamespace. The inner namespace is declared with a C# .Net class WorkItem.

The next logical discussion after a namespace is classes. A class is the basis of object ? oriented programming. It encapsulates the data and methods into one itself and manipulates them through the interaction with that object.

CoderSource.net
C# .Net Tutorial NamespacesRating:


Date: 5/1/2004 12:00:00 AM

.Net C# Tutorial Namespaces
A Namespace in Microsoft .Net is like containers of objects. They may contain unions, classes, structures, interfaces, enumerators and delegates. Main goal of using namespace in .Net is for creating a hierarchical organization of program. In this case a developer does not need to worry about the naming conflicts of classes, functions, variables etc., inside a project.

In Microsoft .Net, every program is created with a default namespace. This default namespace is called as global namespace. But the program itself can declare any number of namespaces, each of them with a unique name. The advantage is that every namespace can contain any number of classes, functions, variables and also namespaces etc., whose names are unique only inside the namespace. The members with the same name can be created in some other namespace without any compiler complaints from Microsoft .Net.




To declare namespace C# .Net has a reserved keyword namespace. If a new project is created in Visual Studio .NET it automatically adds some global namespaces. These namespaces can be different in different projects. But each of them should be placed under the base namespace System. The names space must be added and used through the using operator, if used in a different project.

Please now have a look at the example of declaring some namespace:


--------------------------------------------------------------------------------

using System;
namespace OutNamespace
{

namespace WorkNamespace
{ /// can be placed some classes, structures etc.
}

}


--------------------------------------------------------------------------------

In this example we create two namespaces. These namespaces have hierarchical structure. We have some outer one named OutNamespace and the inner one called WorkNamespace. The inner namespace is declared with a C# .Net class WorkItem.

The next logical discussion after a namespace is classes. A class is the basis of object ? oriented programming. It encapsulates the data and methods into one itself and manipulates them through the interaction with that object.


--------------------------------------------------------------------------------

class WorkItem
{
public WorkItem()
{

}

static WorkItem()
{
m_counter = 1;
}
public static int GetCounter()
{
return m_counter;
}

private static int m_counter;

public virtual void Status()
{

}

internal bool IsWorking
{
get
{
return m_isWorking;
}
set
{
m_isWorking = value;
}
}

private bool m_isWorking = true;

}


The above sample contains the .Net namespace with the class WorkItem inside it.

Thank you for Posting at Dotnetfunda

Were looking Forward for your Reply

Vuyiswa Maseko

Thank you for posting at Dotnetfunda
[Administrator]

Jameschowdare, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Neeks on: 4/17/2009 [Member] Bronze

Up
0
Down
Definition
A namespace is an abstract container providing context for the items. We can also say, it a logical grouping of the names used within a program. We can divide Global scope in sub-scopes using Namespace.

We can define the scope of same class in different ways.

Example.

If you want to use SqlCommand class you have to import System.Data.SqlClient.
Here System.Data.SqlClient is a namespace.

You can also create your namespace to make different operation with the same name.

namespace yourNamespace
{

public class SqlConnection //Please do not import System.Data.SqlClient
{
public SqlConnection()
{
//Do your work
}
}
}


You can call this class by using yourNamespace.SqlConnection.




Jameschowdare, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response