C# 0 The Complete Reference



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

var

with


[ ]

. Furthermore, the array must be initialized because it is the type of the initializers 

that determine the element type of the array. All of the initializers must be of the same or 

compatible type. Here is an example of an implicitly typed array:

var vals = new[] { 1, 2, 3, 4, 5 };

This creates an array of 



int

 that is five elements long. A reference to that array is assigned to 



vals

. Thus, the type of 



vals

 is “array of 



int”

 and it has five elements. Again, notice that 



var

 is 


not followed by 

[ ]

. Also, even though the array is being initialized, you must include 



new[ ]

.

It’s not optional in this context.



Here is another example. It creates a two-dimensional array of 

double

:

var vals = new[,] { {1.1, 2.2}, {3.3, 4.4},{ 5.5, 6.6} };



In this case

vals

 has the dimensions 2×3.

www.freepdf-books.com



152

 

P a r t   I :  



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

You can also declare implicitly typed jagged arrays. For example, consider the following 

program:

// Demonstrate an implicitly typed jagged array.

using System;

class Jagged {

  static void Main() {

    var jagged = new[] {

       new[] { 1, 2, 3, 4 },

       new[] { 9, 8, 7 },

       new[] { 11, 12, 13, 14, 15 }

    };


    for(int j = 0; j < jagged.Length; j++) {

      for(int i=0; i < jagged[j].Length; i++)

        Console.Write(jagged[j][i] + " ");

      Console.WriteLine();

    }

  }


}

The program produces the following output:

1 2 3 4

9 8 7


11 12 13 14 15

Pay special attention to the declaration of 



jagged

:

var jagged = new[] {



   new[] { 1, 2, 3, 4 },

   new[] { 9, 8, 7 },

   new[] { 11, 12, 13, 14, 15 }

};

Notice how 



new[ ]

 is used in two ways. First, it creates the array of arrays. Second, it creates 

each individual array, based on the number and type of initializers. As you would expect

all of the initializers in the individual arrays must be of the same type. The same general 

approach used to declare 

jagged

 can be used to declare any implicitly typed jagged array.

As mentioned, implicitly typed arrays are most applicable to LINQ-based queries. They 

are not meant for general use. In most cases, you should use explicitly typed arrays.



The foreach Loop

In Chapter 5, it was mentioned that C# defines a loop called 



foreach

, but a discussion of 

that statement was deferred until later. The time for that discussion has now come.

The


foreach

 loop is used to cycle through the elements of a 



collection.

 A collection is a 

group of objects. C# defines several types of collections, of which one is an array. The general 

form of 


foreach

 is shown here:

www.freepdf-books.com



Download 4,07 Mb.

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