SlideShare a Scribd company logo
1 of 34
Namespace
• A namespace defines a declarative region that
provides a way to keep one set of names separate
from another.
• Names declared in one namespace will not conflict
with the same names declared in another.
• The namespace used by the .NET Framework
library (which is the C# library) is System.
• This is why you have included near the top of
every program.
using System;
• Namespaces are important because there has
been an explosion of variable, method,
property, and class names over the past few
years.
• These include library routines, third-party code,
and your own code. Without namespaces, all of
these names would compete for slots in the
global namespace and conflicts would arise.
• For example, if your program defined a class
called Finder, it could conflict with another class
called Finder supplied by a third-party library
that your program uses.
• the I/O classes are defined within a
namespace subordinate to System called
System.IO . There are many other namespaces
subordinate to System that hold other parts of
the C# library.
• A namespace is declared using the namespace
keyword. The general form of namespace is
shown here:
namespace name {
// members
}
namespace Counter
{
class CountDown {
int val;
public CountDown(int n) {
public int Count()
{
if (val > 0) return val--;
else return 0;
} }
}

val = n;

}
class Program
{
static void Main(string[] args)
{ int i;
Counter.CountDown cd1 = new
Counter.CountDown(10);
do
{
i = cd1.Count();
Console.Write(i + " ");
} while (i > 0);
Console.WriteLine(); }
}
• Some important aspects of this program
warrant close examination.
• since CountDown is declared within the
Counter namespace, when an object is
created, CountDown must be qualified with
Counter, as shown here:
• Counter.CountDown cd1 = new
Counter.CountDown(10);
Namespaces Prevent Name Conflicts
namespace Counter
{
class CountDown {
int val;
public CountDown(int n) {
public int Count()
{
if (val > 0) return val--;
else return 0;
} } }

val = n;

}
namespace Counter2 {
class CountDown {
public void Count() {
Console.WriteLine("This is Count() in the " +
"Counter2 namespace.");
}
}
}
class NSDemo2 {
static void Main() {
Counter.CountDown cd1 = new
Counter.CountDown(10);
Counter2.CountDown cd2 = new
Counter2.CountDown();
int i;
do {
i = cd1.Count();
Console.Write(i + " ");
} while(i > 0);
Console.WriteLine();
cd2.Count();
}
}

10 9 8 7 6 5 4 3 2 1 0
This is Count() in the Counter2 namespace.
using
• As you would expect, using can also be
employed to bring namespaces that you
create into view.
• There are two forms of the using directive.
The first is shown here:
using name;
• name specifies the name of the namespace
you want to access. This is the form of using
that you have already seen.
• All of the members defined within the
specified namespace are brought into view
and can be used without qualification.
• A using directive must be specified at the top
of each file, prior to any other declarations, or
at the start of a namespace body.
using Counter;
namespace Counter
{
class CountDown {
int val;
public CountDown(int n) {
public int Count()
{
if (val > 0) return val--;
else return 0;
} } }

val = n;

}
class Program
{
static void Main(string[] args)
{ int i;
CountDown cd1 = new CountDown(10);
do
{
i = cd1.Count();
Console.Write(i + " ");
} while (i > 0);
Console.WriteLine(); }
}
A Second Form of using
• The using directive has a second form that
creates another name, called an alias, for a
type or a namespace. This form is shown here:
using alias = name;
• Alias becomes another name for the type
(such as a class type) or namespace specified
byname.
• Once the alias has been created, it can be
used in place of the original name.
using MyCounter = Counter.CountDown;
namespace Counter
{
class CountDown {
int val;
public CountDown(int n) {
val = n;
public int Count()
{
if (val > 0) return val--;
else return 0;
} } }

}
class Program
{
static void Main(string[] args)
{ int i;
MyCounter cd1 = new MyCounter(10);
do
{
i = cd1.Count();
Console.Write(i + " ");
} while (i > 0);
Console.WriteLine(); }
}
• OnceMyCounter has been specified as
another name for Counter.CountDown, it can
be used to declare objects without any
further namespace qualification.
• For example, in the program, this line
MyCounter cd1 = new MyCounter(10);
• creates a CountDown object.
Namespaces Are Additive
• There can be more than one namespace
declaration of the same name.
• This allows a namespace to be split over
several files or even separated within the
same file.
Namespaces Are Additive

using Counter;
namespace Counter{
class CountDown {
int val;
public CountDown(int n)
public int Count()
{
if (val > 0) return val--;
else return 0;
}
}}

{ val = n; }
namespace Counter
{
class CountDo
{
public void CountDo1()
{
Console.WriteLine("this is second namespace
class");
}
}
}
class Program{ static void Main(string[] args)
{int i;
CountDown cd1 = new CountDown(10);
CountDo cd11 = new CountDo();
cd11.CountDo1();
do {
i = cd1.Count();
Console.Write(i + " ");
} while (i > 0);
Console.WriteLine();
}}
Namespaces Can Be Nested
• One namespace can be nested within another.
using System;
namespace NS1 {
class ClassA {
public ClassA() {
Console.WriteLine("constructing ClassA");
}
}
namespace NS2 { // a nested namespace
class ClassB {
public ClassB() {
Console.WriteLine("constructing ClassB");
}
}
}
}
class NestedNSDemo {
static void Main() {
• NS1.ClassA a = new NS1.ClassA();
• // NS2.ClassB b = new NS2.ClassB(); // Error!!!
NS2 is not in view
•

NS1.NS2.ClassB b = new
NS1.NS2.ClassB(); // this is right
• }
• }
Using the :: Namespace Alias Qualifier
• Although namespaces help prevent name
conflicts, they do not completely eliminate
them.
• One way that a conflict can still occur is when
the same name is declared within two
different namespaces, and you then try to
bring both namespaces into view.
• For example, assume that two different
namespaces contain a class called MyClass.
• If you attempt to bring these two namespaces
into view via using statements, MyClass in
the first namespace will conflict with MyClass
in the second namespace, causing an
ambiguity error.
• In this situation, you can use the :: namespace
alias qualifier to explicitly specify which
namespace is intended.
• The :: operator has this general form:
namespace-alias:: identifier
• Here, namespace-alias is the name of a
namespace alias and identifier is the name of
a member of that namespace.
• The trouble is that both namespaces, Counter
and AnotherCounter, declare a class called
CountDown, and both namespaces have been
brought into view.
• Thus, to which version of CountDown does
the preceding declaration refer? The ::
qualifier was designed to handle these types
of problems.
• To use the :: , you must first define an alias
for the namespace you want to qualify. Then,
simply qualify the ambiguous element with
the alias.
using Counter;
using AnotherCounter;
// Give Counter an alias called Ctr.

using Ctr = Counter;
namespace Counter
{
class CountDown
{
int val;
public CountDown(int n) {
val = n;
} }}
namespace AnotherCounter
{
class CountDown {
int val;
public CountDown(int n) {
val = n;
} }
class AliasQualifierDemo {
static void Main() {
// Here, the :: operator
// tells the compiler to use the CountDown
// that is in the Counter namespace.

Ctr::CountDown cd1 = new Ctr::CountDown(10);
// ...
}}
• The use of the :: qualifier removes the
ambiguity because it specifies that the
CountDown in Ctr (which stands for Counter) is
desired, and the program now compiles.

More Related Content

What's hot

Cgroup resource mgmt_v1
Cgroup resource mgmt_v1Cgroup resource mgmt_v1
Cgroup resource mgmt_v1
sprdd
 
Some basic unix commands
Some basic unix commandsSome basic unix commands
Some basic unix commands
aaj_sarkar06
 
Introduction to Unix
Introduction to UnixIntroduction to Unix
Introduction to Unix
Sudharsan S
 

What's hot (20)

Cgroup resource mgmt_v1
Cgroup resource mgmt_v1Cgroup resource mgmt_v1
Cgroup resource mgmt_v1
 
Some basic unix commands
Some basic unix commandsSome basic unix commands
Some basic unix commands
 
Linux commands
Linux commandsLinux commands
Linux commands
 
Linux commands
Linux commandsLinux commands
Linux commands
 
Unix Basics For Testers
Unix Basics For TestersUnix Basics For Testers
Unix Basics For Testers
 
Terminal Commands (Linux - ubuntu) (part-1)
Terminal Commands  (Linux - ubuntu) (part-1)Terminal Commands  (Linux - ubuntu) (part-1)
Terminal Commands (Linux - ubuntu) (part-1)
 
Unix Linux Commands Presentation 2013
Unix Linux Commands Presentation 2013Unix Linux Commands Presentation 2013
Unix Linux Commands Presentation 2013
 
Bootcamp linux commands
Bootcamp linux commandsBootcamp linux commands
Bootcamp linux commands
 
Linux class 8 tar
Linux class 8   tar  Linux class 8   tar
Linux class 8 tar
 
Linux Administrator - The Linux Course on Eduonix
Linux Administrator - The Linux Course on EduonixLinux Administrator - The Linux Course on Eduonix
Linux Administrator - The Linux Course on Eduonix
 
Basic commands
Basic commandsBasic commands
Basic commands
 
Useful Linux and Unix commands handbook
Useful Linux and Unix commands handbookUseful Linux and Unix commands handbook
Useful Linux and Unix commands handbook
 
Linux Commands
Linux CommandsLinux Commands
Linux Commands
 
Quick Guide with Linux Command Line
Quick Guide with Linux Command LineQuick Guide with Linux Command Line
Quick Guide with Linux Command Line
 
Linux basic commands tutorial
Linux basic commands tutorialLinux basic commands tutorial
Linux basic commands tutorial
 
QSpiders - Unix Operating Systems and Commands
QSpiders - Unix Operating Systems  and CommandsQSpiders - Unix Operating Systems  and Commands
QSpiders - Unix Operating Systems and Commands
 
Unix commands in etl testing
Unix commands in etl testingUnix commands in etl testing
Unix commands in etl testing
 
Introduction to Unix
Introduction to UnixIntroduction to Unix
Introduction to Unix
 
Lecture 5 Kernel Development
Lecture 5 Kernel DevelopmentLecture 5 Kernel Development
Lecture 5 Kernel Development
 
Module 02 Using Linux Command Shell
Module 02 Using Linux Command ShellModule 02 Using Linux Command Shell
Module 02 Using Linux Command Shell
 

Viewers also liked

Viewers also liked (12)

Containers and Namespaces in the Linux Kernel
Containers and Namespaces in the Linux KernelContainers and Namespaces in the Linux Kernel
Containers and Namespaces in the Linux Kernel
 
Linux Namespace
Linux NamespaceLinux Namespace
Linux Namespace
 
Linux container, namespaces & CGroup.
Linux container, namespaces & CGroup. Linux container, namespaces & CGroup.
Linux container, namespaces & CGroup.
 
Linux cgroups and namespaces
Linux cgroups and namespacesLinux cgroups and namespaces
Linux cgroups and namespaces
 
Resource Management of Docker
Resource Management of DockerResource Management of Docker
Resource Management of Docker
 
Linux network namespaces
Linux network namespacesLinux network namespaces
Linux network namespaces
 
LSA2 - 02 Namespaces
LSA2 - 02  NamespacesLSA2 - 02  Namespaces
LSA2 - 02 Namespaces
 
Linux Namespaces
Linux NamespacesLinux Namespaces
Linux Namespaces
 
C++11
C++11C++11
C++11
 
Namespaces and cgroups - the basis of Linux containers
Namespaces and cgroups - the basis of Linux containersNamespaces and cgroups - the basis of Linux containers
Namespaces and cgroups - the basis of Linux containers
 
Realizing Linux Containers (LXC)
Realizing Linux Containers (LXC)Realizing Linux Containers (LXC)
Realizing Linux Containers (LXC)
 
Docker, Linux Containers (LXC), and security
Docker, Linux Containers (LXC), and securityDocker, Linux Containers (LXC), and security
Docker, Linux Containers (LXC), and security
 

Similar to Namespace

Presentation 1st
Presentation 1stPresentation 1st
Presentation 1st
Connex
 
18 dec pointers and scope resolution operator
18 dec pointers and scope resolution operator18 dec pointers and scope resolution operator
18 dec pointers and scope resolution operator
SAFFI Ud Din Ahmad
 
Advanced programming topics asma
Advanced programming topics asmaAdvanced programming topics asma
Advanced programming topics asma
AbdullahJana
 

Similar to Namespace (20)

Namespace--defining same identifiers again
Namespace--defining same identifiers againNamespace--defining same identifiers again
Namespace--defining same identifiers again
 
Namespace1
Namespace1Namespace1
Namespace1
 
Csharp generics
Csharp genericsCsharp generics
Csharp generics
 
Constructors & Destructors [Compatibility Mode].pdf
Constructors & Destructors [Compatibility Mode].pdfConstructors & Destructors [Compatibility Mode].pdf
Constructors & Destructors [Compatibility Mode].pdf
 
Constructors and destructors in C++
Constructors and destructors in  C++Constructors and destructors in  C++
Constructors and destructors in C++
 
VP-303 lecture#9.pptx
VP-303 lecture#9.pptxVP-303 lecture#9.pptx
VP-303 lecture#9.pptx
 
Csharp_mahesh
Csharp_maheshCsharp_mahesh
Csharp_mahesh
 
Presentation 1st
Presentation 1stPresentation 1st
Presentation 1st
 
Namespace in C++ Programming Language
Namespace in C++ Programming LanguageNamespace in C++ Programming Language
Namespace in C++ Programming Language
 
18 dec pointers and scope resolution operator
18 dec pointers and scope resolution operator18 dec pointers and scope resolution operator
18 dec pointers and scope resolution operator
 
Synapseindia dot net development
Synapseindia dot net developmentSynapseindia dot net development
Synapseindia dot net development
 
Learn C# at ASIT
Learn C# at ASITLearn C# at ASIT
Learn C# at ASIT
 
CSharp Presentation
CSharp PresentationCSharp Presentation
CSharp Presentation
 
Learn c sharp at amc square learning
Learn c sharp at amc square learningLearn c sharp at amc square learning
Learn c sharp at amc square learning
 
Linq Introduction
Linq IntroductionLinq Introduction
Linq Introduction
 
Chapter 3 part2
Chapter 3  part2Chapter 3  part2
Chapter 3 part2
 
Getting Started - Console Program and Problem Solving
Getting Started - Console Program and Problem SolvingGetting Started - Console Program and Problem Solving
Getting Started - Console Program and Problem Solving
 
SPF Getting Started - Console Program
SPF Getting Started - Console ProgramSPF Getting Started - Console Program
SPF Getting Started - Console Program
 
Java execise
Java execiseJava execise
Java execise
 
Advanced programming topics asma
Advanced programming topics asmaAdvanced programming topics asma
Advanced programming topics asma
 

More from abhay singh (15)

Iso 27001
Iso 27001Iso 27001
Iso 27001
 
Web service
Web serviceWeb service
Web service
 
Unsafe
UnsafeUnsafe
Unsafe
 
Threading
ThreadingThreading
Threading
 
Preprocessor
PreprocessorPreprocessor
Preprocessor
 
Networking and socket
Networking and socketNetworking and socket
Networking and socket
 
Inheritance
InheritanceInheritance
Inheritance
 
Generic
GenericGeneric
Generic
 
Gdi
GdiGdi
Gdi
 
Exception
ExceptionException
Exception
 
Delegate
DelegateDelegate
Delegate
 
Constructor
ConstructorConstructor
Constructor
 
Collection
CollectionCollection
Collection
 
Ado
AdoAdo
Ado
 
Operator overloading
Operator overloadingOperator overloading
Operator overloading
 

Recently uploaded

The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
heathfieldcps1
 
Making and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfMaking and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdf
Chris Hunter
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
ciinovamais
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
QucHHunhnh
 

Recently uploaded (20)

Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SD
 
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-II
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-IIFood Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-II
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-II
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptx
 
Asian American Pacific Islander Month DDSD 2024.pptx
Asian American Pacific Islander Month DDSD 2024.pptxAsian American Pacific Islander Month DDSD 2024.pptx
Asian American Pacific Islander Month DDSD 2024.pptx
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
Energy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural Resources
Energy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural ResourcesEnergy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural Resources
Energy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural Resources
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
 
Making and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfMaking and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdf
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 

Namespace

  • 1. Namespace • A namespace defines a declarative region that provides a way to keep one set of names separate from another. • Names declared in one namespace will not conflict with the same names declared in another. • The namespace used by the .NET Framework library (which is the C# library) is System. • This is why you have included near the top of every program. using System;
  • 2. • Namespaces are important because there has been an explosion of variable, method, property, and class names over the past few years. • These include library routines, third-party code, and your own code. Without namespaces, all of these names would compete for slots in the global namespace and conflicts would arise. • For example, if your program defined a class called Finder, it could conflict with another class called Finder supplied by a third-party library that your program uses.
  • 3. • the I/O classes are defined within a namespace subordinate to System called System.IO . There are many other namespaces subordinate to System that hold other parts of the C# library. • A namespace is declared using the namespace keyword. The general form of namespace is shown here: namespace name { // members }
  • 4. namespace Counter { class CountDown { int val; public CountDown(int n) { public int Count() { if (val > 0) return val--; else return 0; } } } val = n; }
  • 5. class Program { static void Main(string[] args) { int i; Counter.CountDown cd1 = new Counter.CountDown(10); do { i = cd1.Count(); Console.Write(i + " "); } while (i > 0); Console.WriteLine(); } }
  • 6. • Some important aspects of this program warrant close examination. • since CountDown is declared within the Counter namespace, when an object is created, CountDown must be qualified with Counter, as shown here: • Counter.CountDown cd1 = new Counter.CountDown(10);
  • 7. Namespaces Prevent Name Conflicts namespace Counter { class CountDown { int val; public CountDown(int n) { public int Count() { if (val > 0) return val--; else return 0; } } } val = n; }
  • 8. namespace Counter2 { class CountDown { public void Count() { Console.WriteLine("This is Count() in the " + "Counter2 namespace."); } } }
  • 9. class NSDemo2 { static void Main() { Counter.CountDown cd1 = new Counter.CountDown(10); Counter2.CountDown cd2 = new Counter2.CountDown(); int i;
  • 10. do { i = cd1.Count(); Console.Write(i + " "); } while(i > 0); Console.WriteLine(); cd2.Count(); } } 10 9 8 7 6 5 4 3 2 1 0 This is Count() in the Counter2 namespace.
  • 11. using • As you would expect, using can also be employed to bring namespaces that you create into view. • There are two forms of the using directive. The first is shown here: using name;
  • 12. • name specifies the name of the namespace you want to access. This is the form of using that you have already seen. • All of the members defined within the specified namespace are brought into view and can be used without qualification. • A using directive must be specified at the top of each file, prior to any other declarations, or at the start of a namespace body.
  • 13. using Counter; namespace Counter { class CountDown { int val; public CountDown(int n) { public int Count() { if (val > 0) return val--; else return 0; } } } val = n; }
  • 14. class Program { static void Main(string[] args) { int i; CountDown cd1 = new CountDown(10); do { i = cd1.Count(); Console.Write(i + " "); } while (i > 0); Console.WriteLine(); } }
  • 15. A Second Form of using • The using directive has a second form that creates another name, called an alias, for a type or a namespace. This form is shown here: using alias = name; • Alias becomes another name for the type (such as a class type) or namespace specified byname. • Once the alias has been created, it can be used in place of the original name.
  • 16. using MyCounter = Counter.CountDown; namespace Counter { class CountDown { int val; public CountDown(int n) { val = n; public int Count() { if (val > 0) return val--; else return 0; } } } }
  • 17. class Program { static void Main(string[] args) { int i; MyCounter cd1 = new MyCounter(10); do { i = cd1.Count(); Console.Write(i + " "); } while (i > 0); Console.WriteLine(); } }
  • 18. • OnceMyCounter has been specified as another name for Counter.CountDown, it can be used to declare objects without any further namespace qualification. • For example, in the program, this line MyCounter cd1 = new MyCounter(10); • creates a CountDown object.
  • 19. Namespaces Are Additive • There can be more than one namespace declaration of the same name. • This allows a namespace to be split over several files or even separated within the same file.
  • 20. Namespaces Are Additive using Counter; namespace Counter{ class CountDown { int val; public CountDown(int n) public int Count() { if (val > 0) return val--; else return 0; } }} { val = n; }
  • 21. namespace Counter { class CountDo { public void CountDo1() { Console.WriteLine("this is second namespace class"); } } }
  • 22. class Program{ static void Main(string[] args) {int i; CountDown cd1 = new CountDown(10); CountDo cd11 = new CountDo(); cd11.CountDo1(); do { i = cd1.Count(); Console.Write(i + " "); } while (i > 0); Console.WriteLine(); }}
  • 23.
  • 24. Namespaces Can Be Nested • One namespace can be nested within another. using System; namespace NS1 { class ClassA { public ClassA() { Console.WriteLine("constructing ClassA"); } }
  • 25. namespace NS2 { // a nested namespace class ClassB { public ClassB() { Console.WriteLine("constructing ClassB"); } } } } class NestedNSDemo { static void Main() {
  • 26. • NS1.ClassA a = new NS1.ClassA(); • // NS2.ClassB b = new NS2.ClassB(); // Error!!! NS2 is not in view • NS1.NS2.ClassB b = new NS1.NS2.ClassB(); // this is right • } • }
  • 27.
  • 28. Using the :: Namespace Alias Qualifier • Although namespaces help prevent name conflicts, they do not completely eliminate them. • One way that a conflict can still occur is when the same name is declared within two different namespaces, and you then try to bring both namespaces into view. • For example, assume that two different namespaces contain a class called MyClass.
  • 29. • If you attempt to bring these two namespaces into view via using statements, MyClass in the first namespace will conflict with MyClass in the second namespace, causing an ambiguity error. • In this situation, you can use the :: namespace alias qualifier to explicitly specify which namespace is intended.
  • 30. • The :: operator has this general form: namespace-alias:: identifier • Here, namespace-alias is the name of a namespace alias and identifier is the name of a member of that namespace. • The trouble is that both namespaces, Counter and AnotherCounter, declare a class called CountDown, and both namespaces have been brought into view.
  • 31. • Thus, to which version of CountDown does the preceding declaration refer? The :: qualifier was designed to handle these types of problems. • To use the :: , you must first define an alias for the namespace you want to qualify. Then, simply qualify the ambiguous element with the alias.
  • 32. using Counter; using AnotherCounter; // Give Counter an alias called Ctr. using Ctr = Counter; namespace Counter { class CountDown { int val;
  • 33. public CountDown(int n) { val = n; } }} namespace AnotherCounter { class CountDown { int val; public CountDown(int n) { val = n; } }
  • 34. class AliasQualifierDemo { static void Main() { // Here, the :: operator // tells the compiler to use the CountDown // that is in the Counter namespace. Ctr::CountDown cd1 = new Ctr::CountDown(10); // ... }} • The use of the :: qualifier removes the ambiguity because it specifies that the CountDown in Ctr (which stands for Counter) is desired, and the program now compiles.