C# 0 The Complete Reference


CountDown  class inside  Counter



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

CountDown

 class inside 



Counter

 is separate from the 



CountDown

class in the 



Counter2

 namespace, and no name conflicts arise. Although this example is 

quite simple, it is easy to see how putting classes into a namespace helps prevent name 

conflicts between your code and code written by others.



using

If your program includes frequent references to the members of a namespace, having to 

specify the namespace each time you need to refer to one quickly becomes tedious. The 

using

 directive alleviates this problem. Throughout this book, you have been using it to bring 

the C# 

System

 namespace into view, so you are already familiar with it. 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

;

Here, 



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.

The following program reworks the counter example to show how you can employ 

using

 to bring a namespace that you create into view:

// Demonstrate the using directive.

using System;

www.freepdf-books.com



442

 

P a r t   I :  



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

// Bring Counter into view.

using Counter;

// 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 NSDemo3 {



  static void Main() {

    // now, CountDown can be used directly.

    CountDown cd1 = new CountDown(10);

    int i;

    do {

      i = cd1.Count();

      Console.Write(i + " ");

    } while(i > 0);

    Console.WriteLine();

    CountDown cd2 = new CountDown(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();

  }


}

This version of the program contains two important changes. The first is this 



using

statement, near the top of the program:

www.freepdf-books.com



Download 4,07 Mb.

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