C# 0 The Complete Reference


PART I C h a p t e r   1 6



Download 4,07 Mb.
Pdf ko'rish
bet559/1096
Sana23.01.2022
Hajmi4,07 Mb.
#402171
1   ...   555   556   557   558   559   560   561   562   ...   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  

443


PART IPART I

using Counter;

This brings the 

Counter

 namespace into view. The second change is that it is no longer 

necessary to qualify 

CountDown

 with 


Counter

, as this statement in 



Main( )

 shows:


CountDown cd1 = new CountDown(10);

Because


Counter

 is now in view, 



CountDown

 can be used directly.

The program illustrates one other important point: Using one namespace does not 

override another. When you bring a namespace into view, it simply lets you use its contents 

without qualification. Thus, in the example, both 

System

 and 


Counter

have been brought 

into view.

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

;

Here, 



alias

 becomes another name for the type (such as a class type) or namespace specified 

by

name.

 Once the alias has been created, it can be used in place of the original name.

Here the example from the preceding section has been reworked so that an alias for 

Counter.CountDown

 called 


MyCounter

 is created:

// Demonstrate a using alias.

using System;

// Create an alias for Counter.CountDown.

using MyCounter = Counter.CountDown;

// Declare a namespace for counters.

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;

    }

  }


}

class NSDemo4 {

www.freepdf-books.com



444

 

P a r t   I :  



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

  static void Main() {

    // Here, MyCounter is used as a name for Counter.CountDown.

    MyCounter cd1 = new MyCounter(10);

    int i;

    do {


      i = cd1.Count();

      Console.Write(i + " ");

    } while(i > 0);

    Console.WriteLine();

    MyCounter cd2 = new MyCounter(20);

    do {


      i = cd2.Count();

      Console.Write(i + " ");

    } while(i > 0);

    Console.WriteLine();

    cd2.Reset(4);

    do {


      i = cd2.Count();

      Console.Write(i + " ");

    } while(i > 0);

    Console.WriteLine();

  }

}

The



MyCounter

 alias is created using this statement:

using MyCounter = Counter.CountDown;

Once


MyCounter

 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. For example, 

the following program defines two 



Counter

 namespaces. One contains the 



CountDown

class. The other contains the 



CountUp

 class. When compiled, the contents of both 



Counter

namespaces are added together.

// Namespaces are additive.

using System;

// Bring Counter into view.

using Counter;

www.freepdf-books.com



Download 4,07 Mb.

Do'stlaringiz bilan baham:
1   ...   555   556   557   558   559   560   561   562   ...   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