C# 0 The Complete Reference


PART I C h a p t e r   1 6



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


PART I

C h a p t e r   1 6 :  

  N a m e s p a c e s ,   t h e   P r e p r o c e s s o r ,   a n d   A s s e m b l i e s  

449


PART IPART I

    // The following line is inherently ambiguous!

    // Does it refer to CountDown in Counter or

    // to CountDown in AnotherCounter?

    CountDown cd1 = new CountDown(10); // Error! ! !

    // ...

  }

}

If you try to compile this program, you will receive an error message stating that this 



line in 

Main( )

 is ambiguous:

CountDown cd1 = new CountDown(10); // Error! ! !

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. For example, here is one way to fix the 

preceding program:

// Demonstrate the :: qualifier.

using System;

using Counter;

using AnotherCounter;

// Give Counter an alias called Ctr.

using Ctr = Counter;

// Declare a namespace for counters.

namespace Counter {

  // A simple countdown counter.

  class CountDown {

    int val;

    public CountDown(int n) {

      val = n;

    }


    // ...

  }


}

// Another counter namespace.

namespace AnotherCounter {

  // Declare another class called CountDown, which

  // is in the AnotherCounter namespace.

  class CountDown {

    int val;

www.freepdf-books.com




450

 

P a r t   I :  



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

    public CountDown(int n) {

      val = n;

    }


    // ...

  }


}

class AliasQualifierDemo {

  static void Main() {

    // Here, the :: operator to resolve

    // tells the compiler to use the CountDown

    // that is in the Counter namespace.

    Ctr::CountDown cd1 = new Ctr::CountDown(10);

    // ...

  }

}

In this version, the alias 




Download 4,07 Mb.

Do'stlaringiz bilan baham:
1   ...   559   560   561   562   563   564   565   566   ...   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