C# 0 The Complete Reference



Download 4,07 Mb.
Pdf ko'rish
bet808/1096
Sana23.01.2022
Hajmi4,07 Mb.
#402171
1   ...   804   805   806   807   808   809   810   811   ...   1096
Bog'liq
C-Sharp 3 The Complete Reference Herbert Schildt

Using a Predicate

A

predicate

 is a delegate of type 

System.Predicate

 that returns either true or false, based 

upon some condition. It is declared as shown here:

public delegate bool Predicate (T 



obj

)

The object to be tested against the condition is passed in 



obj.

 If 


obj

 satisfies that condition, 

the predicate must return true. Otherwise, it must return false. Predicates are used by 

several methods in 



Array

, including 



Exists( )

,

Find( )

,

FindIndex( )

, and 


FindAll( )

.

The following program demonstrates using a predicate to determine if an array of 



integers contains a negative value. If a negative value is found, the program then obtains the 

first negative value in the array. To accomplish this, the program uses 



Exists( )

 and 


Find( )

.

// Demonstrate Predicate delegate.



using System;

class PredDemo {

  // A predicate method.

  // It returns true if v is negative.

  static bool IsNeg(int v) {

    if(v < 0) return true;

    return false;

  }


  static void Main() {

    int[] nums = { 1, 4, -1, 5, -9 };

    Console.Write("Contents of nums: ");

    foreach(int i in nums)

      Console.Write(i + " ");

    Console.WriteLine();

    // First see if nums contains a negative value.

    if(Array.Exists(nums, PredDemo.IsNeg)) {

      Console.WriteLine("nums contains a negative value.");

      // Now, find first negative value.

      int x = Array.Find(nums, PredDemo.IsNeg);

      Console.WriteLine("First negative value is : " + x);

    }

www.freepdf-books.com




Download 4,07 Mb.

Do'stlaringiz bilan baham:
1   ...   804   805   806   807   808   809   810   811   ...   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