TechyMedia

A .NET Technology blog | .NET,C# Progamming,C# tutorials,WPF WCF|Software Engineer

NAVIGATION - SEARCH

Namespaces In C#

Namespace in C# is logical grouping of related classes.Classes which are related can be logically grouped in named collection called namespace.So a namespace will give unique fully qualified identifier to a class,which avoids name conflicts.There is no physical mapping of namespaces in C#.That is classes with same namespace can scatter across folders,not necessarily be in same folder/project.

Examples of some available namespaces in .NET base class library are,

System

System.Data

System.Text

System.Windows.Forms

System.Collections.Generic;

 

Namespace is not mandatory,but it is optional to avoid name ambiguities.

Namespaces are normally decided as part of project naming conventions as a standard coding practice.For example,

namespace OrgName.ProjName.ModuleName

{

}

Namespace declaration are made as a block with curly braces { ..} , any classes declared within that braces are part of that namespace,

A namespace normally contains classes,delegates,events and exceptions.Also other internal namepsaces can be part of a namespace.

 

Here below namepace PermanantEmployee is an internal namespace within namespace Employee

namespace Employee { Class Employee { } namespace PermanantEmployee { Class PermanantEmployee { } } }

Internal namespaces are used to hide some of your classes and methods from users who access class library. 

using keywords are used for accessing the classes within the namespaces.For example using system on top of the class.using only allows to access the classes and members within that namespace,but not from within internal namespaces.So for accessing sub/internal namespace data of system we need to use the using system.data statement.

some examples of using statements, 

using System;

using System.Data;

using System.Collection.Generic;

using OrgName.ProjName.ModuleName;

Add comment

  Country flag

biuquote
  • Comment
  • Preview
Loading