Csharp/CSharp Tutorial/Data Structure/Array Exist Find

Материал из .Net Framework эксперт
Перейти к: навигация, поиск

Demonstrate Predicate delegate

using System;   
  
class MainClass {      
 
  static bool isCriteria(int v) { 
    if(v > 1) 
      return true; 
    return false; 
  } 
 
  public 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();  
  
    if(Array.Exists(nums, isCriteria)) { 
      Console.WriteLine("nums contains a negative value."); 
 
      // Now, find first negative value. 
      int x = Array.Find(nums, isCriteria); 
      Console.WriteLine("First negative value is : " + x); 
    } 
  }      
}
Contents of nums: 1 4 -1 5 -9
nums contains a negative value.
First negative value is : 4