C# 0 The Complete Reference


foreach  cycles through the array one element at a time, from start to finish. One important point to remember about  foreach



Download 4,07 Mb.
Pdf ko'rish
bet218/1096
Sana23.01.2022
Hajmi4,07 Mb.
#402171
1   ...   214   215   216   217   218   219   220   221   ...   1096
Bog'liq
C-Sharp 3 The Complete Reference Herbert Schildt

foreach

 cycles through the array one element at a time, from start to finish.

One important point to remember about 

foreach

 is that the iteration variable 



loopvar

 is 


read-only. This means you can’t change the contents of an array by assigning the iteration 

variable a new value.

Here is a simple example that uses 

foreach

. It creates an array of integers and gives it 

some initial values. It then displays those values, computing the summation in the process.

// Use the foreach loop.

using System;

class ForeachDemo {

  static void Main() {

    int sum = 0;

    int[] nums = new int[10];

    // Give nums some values.

    for(int i = 0; i < 10; i++)

      nums[i] = i;

    // Use foreach to display and sum the values.

    foreach(int x in nums) {

      Console.WriteLine("Value is: " + x);

      sum += x;

    }

    Console.WriteLine("Summation: " + sum);



  }

}

The output from the program is shown here:



Value is: 0

Value is: 1

Value is: 2

Value is: 3

Value is: 4

Value is: 5

Value is: 6

Value is: 7

www.freepdf-books.com



154

 

P a r t   I :  



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

Value is: 8

Value is: 9

Summation: 45

As this output shows, 


Download 4,07 Mb.

Do'stlaringiz bilan baham:
1   ...   214   215   216   217   218   219   220   221   ...   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