C# 0 The Complete Reference



Download 4,07 Mb.
Pdf ko'rish
bet556/1096
Sana23.01.2022
Hajmi4,07 Mb.
#402171
1   ...   552   553   554   555   556   557   558   559   ...   1096
Bog'liq
C-Sharp 3 The Complete Reference Herbert Schildt

R

EMEMBER

R

EMEMBER

For clarity, the remaining namespace examples in this chapter show all namespaces 

required by a program within the same file. In real-world code, however, a namespace will often 

be defined in its own file, as the preceding example illustrates.

Namespaces Prevent Name Conflicts

The key point about a namespace is that names declared within it won’t conflict with 

similar names declared outside of it. For example, the following program defines two 

namespaces. The first is 



Counter

, shown earlier. The second is called 



Counter2

. Both 


contain classes called 

CountDown

, but because they are in separate namespaces, the two 

classes do not conflict. Also notice how both namespaces are specified within the same file. 

As just explained, a single file can contain multiple namespace declarations. Of course, 

separate files for each namespace could also have been used.

// Namespaces prevent name conflicts.

using System;

// Declare the Counter namespace.

namespace Counter {

  // A simple countdown counter.

  class CountDown {

    int val;

    public CountDown(int n) {

      val = n;

    }

    public void Reset(int n) {



      val = n;

    }


    public int Count() {

      if(val > 0) return val--;

      else return 0;

    }


  }

}

// Declare the Counter2 namespace.



namespace Counter2 {

  /* This CountDown is in the Counter2 namespace and

     does not conflict with the one in Counter. */

  class CountDown {

    public void Count() {

      Console.WriteLine("This is Count() in the " +

                        "Counter2 namespace.");

    }


www.freepdf-books.com


Download 4,07 Mb.

Do'stlaringiz bilan baham:
1   ...   552   553   554   555   556   557   558   559   ...   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