C# 0 The Complete Reference


NS2  is nested within  NS1



Download 4,07 Mb.
Pdf ko'rish
bet562/1096
Sana23.01.2022
Hajmi4,07 Mb.
#402171
1   ...   558   559   560   561   562   563   564   565   ...   1096
Bog'liq
C-Sharp 3 The Complete Reference Herbert Schildt

NS2

 is nested within 



NS1

. Thus, to refer to 



ClassB

, you 


must qualify it with both the 

NS1

 and 


NS2

 namespaces. 



NS2

, by itself, is insufficient. As 

shown, the namespace names are separated by a period. Therefore, to refer to 

ClassB

 within 


Main( )

, you must use 



NS1.NS2.ClassB

.

Namespaces can be nested by more than two levels. When this is the case, a member in 



a nested namespace must be qualified with all of the enclosing namespace names.

You can specify a nested namespace using a single 



namespace

 statement by separating 

each namespace with a period. For example,

namespace OuterNS {

  namespace InnerNS {

    // ...

  }

}

can also be specified like this:



namespace OuterNS.InnerNS {

  // ...


}

The Global Namespace

If you don’t declare a namespace for your program, then the default global namespace is 

used. This is why you have not needed to use 

namespace

 for the programs in the preceding 

chapters. Although the global namespace is convenient for the short, sample programs 

found in this book, most real-world code will be contained within a declared namespace. 

The main reason for encapsulating your code within a declared namespace is that it 

prevents name conflicts. Namespaces are another tool that you have to help you organize 

programs and make them viable in today’s complex, networked environment.

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.

www.freepdf-books.com



448

 

P a r t   I :  



T h e   C #   L a n g u a g e

The :: operator has this general form.



namespace-alias

::

identifi er

Here, 

namespace-alias

is the name of a namespace alias and 



identifier

 is the name of a member 

of that namespace.

To understand why the namespace alias qualifier is needed, consider the following 

program. It creates two namespaces, 

Counter

 and 


AnotherCounter

, and both declare a class 

called

CountDown

. Furthermore, both namespaces are brought into view by 



using

 statements. 

Finally, in 

Main( )

, an attempt is made to instantiate an object of type 



CountDown

.

// Demonstrate why the :: qualifier is needed.



//

// This program will not compile.

using System;

// Use both the Counter and AnotherCounter namespace.

using Counter;

using AnotherCounter;

// Declare a namespace for counters.

namespace Counter {

  // A simple countdown counter.

  class CountDown {

    int val;

    public CountDown(int n) {

      val = n;

    }


    // ...

  }


}

// Declare another namespace for counters.

namespace AnotherCounter {

  // Declare another class called CountDown, which

  // is in the AnotherCounter namespace.

  class CountDown {

    int val;

    public CountDown(int n) {

      val = n;

    }


    // ...

  }


}

class WhyAliasQualifier {

  static void Main() {

    int i;

www.freepdf-books.com



Download 4,07 Mb.

Do'stlaringiz bilan baham:
1   ...   558   559   560   561   562   563   564   565   ...   1096




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish