Demonstrate Predicate delegate : Array Exist Find « Data Structure « C# / CSharp Tutorial






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








11.14.Array Exist Find
11.14.1.Demonstrate Predicate delegate
11.14.2.Use Array.Exists to check if a certain element exist
11.14.3.Use Array.Find to find array elements end with s
11.14.4.Use Array.FindLast to find the string element ends with s
11.14.5.Array.FindAll
11.14.6.All three generic overloads of the Array.IndexOf method.
11.14.7.Array.LastIndexOf demo
11.14.8.Using Array.TrueForAll for testing if array elements all end with string s