C# 0 The Complete Reference


Storing User-Defined Classes in Collections



Download 4,07 Mb.
Pdf ko'rish
bet990/1096
Sana23.01.2022
Hajmi4,07 Mb.
#402171
1   ...   986   987   988   989   990   991   992   993   ...   1096
Bog'liq
C-Sharp 3 The Complete Reference Herbert Schildt

Storing User-Defined Classes in Collections

For the sake of simplicity, the foregoing examples have stored built-in types, such as 



int

,

string

, or 

char

, in a collection. Of course, collections are not limited to the storage of built-in 

objects. Quite the contrary. The power of collections is that they can store any type of object, 

including objects of classes that you create.

Let’s begin with an example that uses the non-generic class 

ArrayList

 to store inventory 

information that is encapsulated by the 

Inventory

 class:


// A simple inventory example.

using System;

using System.Collections;

class Inventory {

  string name;

  double cost;

  int onhand;

  public Inventory(string n, double c, int h) {

    name = n;

    cost = c;

    onhand = h;

www.freepdf-books.com




800

 

P a r t   I I :  



E x p l o r i n g   t h e   C #   L i b r a r y

  }


  public override string ToString() {

    return

      String.Format("{0,-10}Cost: {1,6:C}  On hand: {2}",

                    name, cost, onhand);

  }

}

class InventoryList {



  static void Main() {

    ArrayList inv = new ArrayList();

    // Add elements to the list

    inv.Add(new Inventory("Pliers", 5.95, 3));

    inv.Add(new Inventory("Wrenches", 8.29, 2));

    inv.Add(new Inventory("Hammers", 3.50, 4));

    inv.Add(new Inventory("Drills", 19.88, 8));

    Console.WriteLine("Inventory list:");

    foreach(Inventory i in inv) {

      Console.WriteLine("   " + i);

    }

  }


}

The output from the program is shown here:

Inventory list:

   Pliers    Cost:  $5.95  On hand: 3

   Wrenches  Cost:  $8.29  On hand: 2

   Hammers   Cost:  $3.50  On hand: 4

   Drills    Cost: $19.88  On hand: 8

In the program, notice that no special actions were required to store objects of type 



Inventory

 in a collection. Because all types inherit 



object

, any type of object can be stored in 

any non-generic collection. Thus, using a non-generic collection, it is trivially easy to store 

objects of classes that you create. Of course, it also means the collection is not type-safe.

To store objects of classes that you create in a type-safe collection, you must use one of 

the generic collection classes. For example, here is a version of the preceding program 

rewritten to use 

List

. The output is the same as before.

// Store Inventory Objects in a List collection.

using System;

using System.Collections.Generic;

class Inventory {

  string name;

  double cost;

  int onhand;

  public Inventory(string n, double c, int h) {

    name = n;

www.freepdf-books.com




Download 4,07 Mb.

Do'stlaringiz bilan baham:
1   ...   986   987   988   989   990   991   992   993   ...   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