This print for content only-size & color not accurate 7" X 9-1/4" / casebound / malloy


// Indexers allow you to access items in an array-like fashion



Download 21,46 Mb.
Pdf ko'rish
bet757/1830
Sana28.09.2021
Hajmi21,46 Mb.
#188411
1   ...   753   754   755   756   757   758   759   760   ...   1830
Bog'liq
[book] [en] Pro C# 2008 and the NET 3.5 Platform Fourth Edition

// Indexers allow you to access items in an array-like fashion.

class Program

{

static void Main(string[] args)



{

Console.WriteLine("***** Fun with Indexers *****\n");

PeopleCollection myPeople = new PeopleCollection();

// Add objects with indexer syntax.

myPeople[0] = new Person("Homer", "Simpson", 40);

myPeople[1] = new Person("Marge", "Simpson", 38);

myPeople[2] = new Person("Lisa", "Simpson", 9);

myPeople[3] = new Person("Bart", "Simpson", 7);

myPeople[4] = new Person("Maggie", "Simpson", 2);



// Now obtain and display each item using indexer.

for (int i = 0; i < myPeople.Count; i++)

{

Console.WriteLine("Person number: {0}", i);



Console.WriteLine("Name: {0} {1}",

myPeople[i].FirstName, myPeople[i].LastName);

Console.WriteLine("Age: {0}", myPeople[i].Age);

Console.WriteLine();

}

}

}



As you can see, indexers behave much like a custom collection supporting the IEnumerator and

IEnumerable interfaces in that they provide access to a container’s subitems. The major difference

of course is that rather than accessing the contents using the foreach construct, you are able to

manipulate the internal collection of sub-objects just like a standard array.

Now for the big question: How do you configure the PeopleCollection class (or any class/

structure) to support this functionality? An indexer is represented as a slightly mangled C# property.

In its simplest form, an indexer is created using the this[] syntax. Here is the required update for

the PeopleCollection class:




Download 21,46 Mb.

Do'stlaringiz bilan baham:
1   ...   753   754   755   756   757   758   759   760   ...   1830




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