C# | Array.FindLast() Method (original) (raw)
Last Updated : 17 Dec, 2021
This method is used to search for an element that matches the conditions defined by the specified predicate and returns the last occurrence within the entire Array.Syntax:
public static T FindLast (T[] array, Predicate match);
Parameters:
array: It is the one-dimensional, zero-based array to search.match: It is the predicate that defines the conditions of the element to search for.
Return Value: This method returns the last element that matches the conditions defined by the specified predicate if it is found otherwise returns the default value for type T.Exception: This method throws ArgumentNullException if the array is null or match is null. Below programs illustrate the use of Array.FindLast(T[], Predicate) Method:Example 1:
CSHARP `
// C# program to demonstrate // Array.FindLast(T[], Predicate) // Method using System; using System.Collections.Generic;
public class GFG {
// Main Method
public static void Main()
{
try {
// Creating and initializing
// new the String
String[] myArr = {"Sun", "Son", "Tue", "Thu"};
// Display the values of the myArr.
Console.WriteLine("Initial Array:");
// calling the PrintIndexAndValues()
// method to print
PrintIndexAndValues(myArr);
// getting a last element with required
// condition using method FindLast()
string value = Array.FindLast(myArr,
element => element.StartsWith("S",
StringComparison.Ordinal));
// Display the value of
// the found element.
Console.Write("Last occurrence: ");
// printing the string
// following the condition
Console.Write("{0}", value);
}
catch (ArgumentNullException e) {
Console.Write("Exception Thrown: ");
Console.Write("{0}", e.GetType(), e.Message);
}
}
// Defining the method
// PrintIndexAndValues
public static void PrintIndexAndValues(String[] myArr)
{
for (int i = 0; i < myArr.Length; i++) {
Console.WriteLine("{0}", myArr[i]);
}
Console.WriteLine();
}
}
`
Output:
Initial Array: Sun Son Tue Thu
Last occurrence: Son
Example 2:
CSHARP `
// C# program to demonstrate // Array.FindLast(T[], Predicate) // Method using System; using System.Collections.Generic;
public class GFG {
// Main Method
public static void Main()
{
try {
// Creating and initializing
// new Array String with null
String[] myArr = null;
// getting a last element with required
// condition using method FindLast()
string value = Array.FindLast(myArr,
element => element.StartsWith("S",
StringComparison.Ordinal));
// Display the value of
// the found element.
Console.Write("Last occurrence: ");
// printing the string
// following the condition
Console.Write("{0}", value);
}
catch (ArgumentNullException e) {
Console.Write("Exception Thrown: ");
Console.Write("{0}", e.GetType(), e.Message);
}
}
// Defining the method
// PrintIndexAndValues
public static void PrintIndexAndValues(String[] myArr)
{
for (int i = 0; i < myArr.Length; i++) {
Console.WriteLine("{0}", myArr[i]);
}
Console.WriteLine();
}
}
`
Output:
Exception Thrown: System.ArgumentNullException
Reference:
Similar Reads
- C# | Array.Find() Method This method is used to search for an element that matches the conditions defined by the specified predicate and returns the first occurrence within the entire Array. Syntax: public static T Find (T[] array, Predicate match); Here, T is the type of element of the array. Parameters: array: It 3 min read
- Array.LastIndexOf Method in C# | Set - 1 Array.LastIndexOf method is used to find the last matching element in a one-dimensional array. It starts the search from the last element of an array. It returns the index of the element that contains the specified value. There are 6 methods in the overload list of this method as follows: LastIndexO 8 min read
- Array.LastIndexOf Method in C# | Set – 2 Array.LastIndexOf method is used to find the index of the last occurrence of a value in a one-dimensional Array or in a portion of the Array. It starts the search from the last element of an array. It returns the index of the element that contains the specified value. There are 6 methods in the over 4 min read
- C# | Array.LastIndexOf(T[], T) Method Array.LastIndexOf(T[], T) Method is used to search for the specified object. It returns the index of the last occurrence within the entire Array. Syntax: public static int LastIndexOf(T[] array, T value); Parameters: array: It is a one-dimensional, zero-indexed array to search. val 2 min read
- C# | Finding the index of last element in the array GetUpperBound() Method is used to find the index of the last element of the specified dimension in the array. Syntax: public int GetUpperBound (int dimension); Here, dimension is a zero-based dimension of the array whose upper bound needs to be determined. Return Value:The return type of this method 2 min read
- C# | Finding the index of first element in the array GetLowerBound() Method is used to find the index of the first element of the specified dimension in the array. Syntax: public int GetLowerBound (int dimension); Here, dimension is a zero-based dimension of the array whose lower bound needs to be determined. Return Value: The return type of this meth 2 min read
- C# | Array.BinarySearch(Array, Object, IComparer) Method This method searches for a value in a one-dimensional sorted array using a specified IComparer interface. Syntax: public static int BinarySearch(Array arr, Object val, IComparer comparer) Parameters: arr : The one-dimensional sorted array in which the search will happen. val : The object value which 4 min read
- C# | Array.BinarySearch(Array, Int32, Int32, Object, IComparer) Method This method searches for an element which is in a one-dimensional sorted array within a range of elements using a specified IComparer interface. Syntax: public static int BinarySearch(Array arr, int index, int length, Object value, IComparer comparer) Parameters: arr : The sorted one-dimensional Arr 4 min read
- Array.BinarySearch(Array, Int32, Int32, Object) Method with examples in C# Array.BinarySearch(Array, int32, int32, Object) Method is used to search a range of elements in a one-dimensional sorted array for a value, using the IComparable interface implemented by each element of the array and by the specified value. It searches only in a specified boundary that the user defi 4 min read
- C# | Get the last node of the LinkedList LinkedList.Last property is used to get the last node of the LinkedList. Syntax: public System.Collections.Generic.LinkedListNode Last { get; } Return Value: The last LinkedListNode of the LinkedList. Below given are some examples to understand the implementation 2 min read