C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Here: This code loops through each int value in the List, starting at the beginning, and tests each one to see if it is greater than 20.
Return: The value 23 is returned. The parameter to the Find method is a lambda expression: a Predicate instance.
C# program that uses Find method on List
using System;
using System.Collections.Generic;
class Program
{
static void Main()
{
List<int> list = new List<int>(new int[] { 19, 23, 29 });
// Finds first element greater than 20
int result = list.Find(item => item > 20);
Console.WriteLine(result);
}
}
Output
23
C# program that uses FindLast
using System;
using System.Collections.Generic;
class Program
{
static void Main()
{
var list = new List<int>(new int[] { 19, 23, 29 });
// Find last element greater than 20.
int result = list.FindLast(item => item > 20);
Console.WriteLine("FINDLAST: {0}", result);
}
}
Output
FINDLAST: 29
Example: The FindIndex call here finds the first element greater than or equal to 100. This is 700, which is at the index 2.
Not found: If the element is not found, the special value -1 is returned. We must test for this value in most code that uses FindIndex.
C# program that uses FindIndex
using System;
using System.Collections.Generic;
class Program
{
static void Main()
{
var list = new List<int>() { 0, 1, 700, 0 };
// Find index of element 100 or greater.
int result = list.FindIndex(element => element >= 100);
Console.WriteLine("FINDINDEX: {0}", result);
}
}
Output
FINDINDEX: 2
Program: Here we invoke FindAll in a foreach-loop condition. FindAll is only called once at the start of the foreach-loop.
Result: We match all strings starting with the lowercase "b," and print them to the console in the body of the loop.
StartsWith, EndsWithConsoleC# program that invokes FindAll
using System;
using System.Collections.Generic;
class Program
{
static void Main()
{
var list = new List<string>() { "bird", "frog", "ball", "leaf" };
// Invoke FindAll info reach.
// ... FindAll is only invoked once for the entire loop.
foreach (string value in list.FindAll(element => element.StartsWith("b")))
{
Console.WriteLine("FINDALL, STARTSWITH B: {0}", value);
}
}
}
Output
FINDALL, STARTSWITH B: bird
FINDALL, STARTSWITH B: ball
Exists call 1: The code tests first to see if any element in the List exists that has a value greater than 10, which returns true.
Exists call 2: Then it tests for values less than 7, which returns false. We can see that Exists returns true or false.
True, FalseC# program that uses Exists method on List
using System;
using System.Collections.Generic;
class Program
{
static void Main()
{
List<int> list = new List<int>();
list.Add(7);
list.Add(11);
list.Add(13);
// See if any elements with values greater than 10 exist.
bool exists = list.Exists(element => element > 10);
Console.WriteLine(exists);
// Check for numbers less than 7.
exists = list.Exists(element => element < 7);
Console.WriteLine(exists);
}
}
Output
True
False