C# 0 The Complete Reference


PART II C h a p t e r   2 1



Download 4,07 Mb.
Pdf ko'rish
bet797/1096
Sana23.01.2022
Hajmi4,07 Mb.
#402171
1   ...   793   794   795   796   797   798   799   800   ...   1096
Bog'liq
C-Sharp 3 The Complete Reference Herbert Schildt


PART II

C h a p t e r   2 1 :  

E x p l o r i n g   t h e   S y s t e m   N a m e s p a c e    

641


The Array Class

One very useful class in 



System

 is 


Array

.

Array

 is a base class for all arrays in C#. Thus, its 

methods can be applied to arrays of any of the built-in types or to arrays of types that you 

create. 

Array

 defines the properties shown in Table 21-11. It defines the methods shown in 

Table 21-12.

Array

 implements the following interfaces: 



ICloneable

,

ICollection

,

IEnumerable

, and 


IList

.

ICollection

,

IEnumerable

, and 


IList

 are defined in the 



System.Collections

 namespace 

and are described in Chapter 24.

Several methods use a parameter of type 



IComparer

or

IComparer

. The 

IComparer

interface is in 



System.Collections

. It defines a method called 



Compare( )

, which compares 

the values of two objects. It is shown here:

int Compare(object 



v1

, object 



v2

)

It returns greater than zero if 



v1

 is greater than 



v2,

 less than zero if 



v1

 is less than 



v2,

 and 


zero if the two values are equal.

IComparer

 is in 


System.Collections.Generic

. It defines a generic form of 



Compare( )

,

which is shown here:



int Compare(T 

v1

, T 


v2

)

It works the same as its non-generic relative: returning greater than zero if 



v1

 is greater than 



v2,

 less than zero if 



v1

 is less than 



v2,

 and zero if the two values are equal. The advantage to 



IComparer

 is type safety, because the type of data being operated upon is explicitly 

specified. Thus, no casts from 

object

 are required.

The next few sections demonstrate several commonly used array operations.

public static bool Tr yParse(string 



str

,

                                        out bool 



b

)

Attempts to conver t the character in 



str

 into its 

bool

equivalent. If successful, the value is stored in 



b

 and true 

is returned. If the string is neither 

Boolean.TrueString

 nor 

Boolean.FalseString



, false is returned. (Case differences 

are ignored.) This differs from 

Parse( )

, which throws an 

exception on failure.

T

ABLE



 21-10 

Methods Defi ned by 

Boolean

(continued)

Method


Meaning

Property


Meaning

public bool IsFixedSize { get; }

A read-only proper ty that is true if the array is of fixed size and 

false if the array is dynamic. This value is true for arrays.

public bool IsReadOnly { get; }

A read-only proper ty that is true if the 

Array

 object is read-only 



and false if it is not. This value is true for arrays.

public bool IsSynchronized { get; }

A read-only proper ty that is true if the array is safe for use in a 

multithreaded environment and false if it is not. This value is 

true for arrays.

T

ABLE



 21-11 

Proper ties Defi ned by 

Array

www.freepdf-books.com




642

 

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 int Length { get; }

An 

int


 read-only proper ty that contains the number of elements 

in the array.

public long LongLength { get; }

long



 read-only proper ty that contains the number of elements 

in the array.

public int Rank { get; }

A read-only proper ty that contains the number of dimensions in 

the array.

public object SyncRoot { get; }

A read-only proper ty that contains the object that synchronizes 

access to the array.

T

ABLE


 21-11 

Proper ties Defi ned by 

Array

(continued)

Property


Meaning

Method


Meaning

public static ReadOnlyCollection

     AsReadOnly(T[ ] 

a

)

Returns a read-only collection that wraps the array specified by 



a.

public static int Binar ySearch(Array 



a

, object 



v

)

Searches the array specified by 



a

 for the value specified by 



v.

 Returns the 

index of the first match. If 

v

 is not found, returns a negative value. The array 

must be sor ted and one-dimensional.

public static int Binar ySearch(T[ ] 



a

, T 


v

)

Searches the array specified by 



a

 for the value specified by 



v.

 Returns the 

index of the first match. If 

v

 is not found, returns a negative value. The array 

must be sor ted and one-dimensional. 

public static int

    Binar ySearch(Array 

a

, object 



v

,

                        IComparer 



comp

)

Searches the array specified by 



a

 for the value specified by 



v,

 using the 

comparison method specified by 

comp.

 Returns the index of the first match. 

If

v

 is not found, returns a negative value. The array must be sor ted and one-

dimensional.

public static int

    Binar ySearch(T[ ] 

a

, T 


v

,

                         IComparer 



comp

)

Searches the array specified by 



a

 for the value specified by 



v,

 using the 

comparison method specified by 

comp.

 Returns the index of the first match. 

If

v

 is not found, returns a negative value. The array must be sor ted and one-

dimensional.

public static int

    Binar ySearch(Array 

a

, int 


star t

,

                        int 



count

, object 



v

)

Searches a por tion of the array specified by 



a

 for the value specified by 



v.

The search begins at the index specified by 



star t

 and is restricted to 



count

elements. Returns the index of the first match. If 



v

 is not found, returns a 

negative value. The array must be sor ted and one-dimensional.

public static int

    Binar ySearch(T[ ] 

a

, int 


star t

,

                        int 



count

, T 


v

)

Searches a por tion of the array specified by 



a

 for the value specified by 



v.

The search begins at the index specified by 



star t

 and is restricted to 



count

elements. Returns the index of the first match. If 



v

 is not found, returns a 

negative value. The array must be sor ted and one-dimensional.

public static int

    Binar ySearch(Array 

a

, int 


star t

,

                        int 



count

, object 



v

,

                        IComparer 



comp

)

Searches a por tion of the array specified by 



a

 for the value specified by 



v,

using the comparison method specified by 



comp.

 The search begins at the 

index specified by 

star t

 and is restricted to 



count

 elements. Returns the 

index of the first match. If 

v

 is not found, returns a negative value. The array 

must be sor ted and one-dimensional.

public static int

    Binar ySearch(T [ ] 

a

, int 


star t

,

                        int 



count

, T 


v

,

                        IComparer 



comp

)

Searches a por tion of the array specified by 



a

 for the value specified by 



v,

using the comparison method specified by 



comp.

 The search begins at the 

index specified by 

star t

 and is restricted to 



count

 elements. Returns the 

index of the first match. If 

v

 is not found, returns a negative value. The array 

must be sor ted and one-dimensional.

T

ABLE



 21-12 

Methods Defi ned by 

Array

www.freepdf-books.com




Download 4,07 Mb.

Do'stlaringiz bilan baham:
1   ...   793   794   795   796   797   798   799   800   ...   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