# Array.FindLast(T[], Predicate<T>)Array.FindLast(T[], Predicate<T>)Array.FindLast(T[], Predicate<T>)Array.FindLast(T[], Predicate<T>) Method

## 定義

``````public:
generic <typename T>
static T FindLast(cli::array <T> ^ array, Predicate<T> ^ match);``````
``public static T FindLast<T> (T[] array, Predicate<T> match);``
``static member FindLast : 'T[] * Predicate<'T> -> 'T``
``Public Shared Function FindLast(Of T) (array As T(), match As Predicate(Of T)) As T``

T

#### 參數

array
T[]

match
Predicate<T> Predicate<T> Predicate<T> Predicate<T>

T T T T

#### 例外狀況

`array``null``array` is `null`.

-或--or- `match``null``match` is `null`.

## 範例

Find泛型方法會從一開始，再傳遞給每個項目來周遊陣列`EndsWithSaurus`方法。The Find generic method traverses the array from the beginning, passing each element in turn to the `EndsWithSaurus` method. 搜尋停止的時機`EndsWithSaurus`方法會傳回`true`項目的 「 Amargasaurus"。The search stops when the `EndsWithSaurus` method returns `true` for the element "Amargasaurus".

FindLast泛型的方法用來搜尋從結尾回溯陣列。The FindLast generic method is used to search the array backward from the end. 它會尋找的項目"Dilophosaurus 」 在位置 5。It finds the element "Dilophosaurus" at position 5. FindAll泛型的方法用來傳回包含"saurus"結尾的所有項目的陣列。The FindAll generic method is used to return an array containing all the elements that end in "saurus". 所顯示的項目。The elements are displayed.

``````using namespace System;

public ref class DinoDiscoverySet
{
public:
static void Main()
{
array<String^>^ dinosaurs =
{
"Compsognathus", "Amargasaurus", "Oviraptor",
"Velociraptor",  "Deinonychus",  "Dilophosaurus",
"Gallimimus",    "Triceratops"
};

DinoDiscoverySet^ GoMesozoic = gcnew DinoDiscoverySet(dinosaurs);

GoMesozoic->DiscoverAll();
GoMesozoic->DiscoverByEnding("saurus");
}

DinoDiscoverySet(array<String^>^ items)
{
dinosaurs = items;
}

void DiscoverAll()
{
Console::WriteLine();
for each(String^ dinosaur in dinosaurs)
{
Console::WriteLine(dinosaur);
}
}

void DiscoverByEnding(String^ Ending)
{
Predicate<String^>^ dinoType;

if (Ending->ToLower() == "raptor")
{
dinoType =
gcnew Predicate<String^>(&DinoDiscoverySet::EndsWithRaptor);
}
else if (Ending->ToLower() == "tops")
{
dinoType =
gcnew Predicate<String^>(&DinoDiscoverySet::EndsWithTops);
}
else if (Ending->ToLower() == "saurus")
{
dinoType =
gcnew Predicate<String^>(&DinoDiscoverySet::EndsWithSaurus);
}
else
{
dinoType =
gcnew Predicate<String^>(&DinoDiscoverySet::EndsWithSaurus);
}

Console::WriteLine(
"\nArray::Exists(dinosaurs, \"{0}\"): {1}",
Ending,
Array::Exists(dinosaurs, dinoType));

Console::WriteLine(
"\nArray::TrueForAll(dinosaurs, \"{0}\"): {1}",
Ending,
Array::TrueForAll(dinosaurs, dinoType));

Console::WriteLine(
"\nArray::Find(dinosaurs, \"{0}\"): {1}",
Ending,
Array::Find(dinosaurs, dinoType));

Console::WriteLine(
"\nArray::FindLast(dinosaurs, \"{0}\"): {1}",
Ending,
Array::FindLast(dinosaurs, dinoType));

Console::WriteLine(
"\nArray::FindAll(dinosaurs, \"{0}\"):", Ending);

array<String^>^ subArray =
Array::FindAll(dinosaurs, dinoType);

for each(String^ dinosaur in subArray)
{
Console::WriteLine(dinosaur);
}
}

private:
array<String^>^ dinosaurs;

// Search predicate returns true if a string ends in "saurus".
static bool EndsWithSaurus(String^ s)
{
if ((s->Length > 5) &&
(s->Substring(s->Length - 6)->ToLower() == "saurus"))
{
return true;
}
else
{
return false;
}
}

// Search predicate returns true if a string ends in "raptor".
static bool EndsWithRaptor(String^ s)
{
if ((s->Length > 5) &&
(s->Substring(s->Length - 6)->ToLower() == "raptor"))
{
return true;
}
else
{
return false;
}
}

// Search predicate returns true if a string ends in "tops".
static bool EndsWithTops(String^ s)
{
if ((s->Length > 3) &&
(s->Substring(s->Length - 4)->ToLower() == "tops"))
{
return true;
}
else
{
return false;
}
}
};

int main()
{
DinoDiscoverySet::Main();
}

/* This code example produces the following output:

Compsognathus
Amargasaurus
Oviraptor
Velociraptor
Deinonychus
Dilophosaurus
Gallimimus
Triceratops

Array.Exists(dinosaurs, "saurus"): True

Array.TrueForAll(dinosaurs, "saurus"): False

Array.Find(dinosaurs, "saurus"): Amargasaurus

Array.FindLast(dinosaurs, "saurus"): Dilophosaurus

Array.FindAll(dinosaurs, "saurus"):
Amargasaurus
Dilophosaurus
*/
``````
``````using System;

public class DinoDiscoverySet
{
public static void Main()
{
string[] dinosaurs =
{
"Compsognathus", "Amargasaurus", "Oviraptor",
"Velociraptor",  "Deinonychus",  "Dilophosaurus",
"Gallimimus",    "Triceratops"
};

DinoDiscoverySet GoMesozoic = new DinoDiscoverySet(dinosaurs);

GoMesozoic.DiscoverAll();
GoMesozoic.DiscoverByEnding("saurus");
}

private string[] dinosaurs;

public DinoDiscoverySet(string[] items)
{
dinosaurs = items;
}

public void DiscoverAll()
{
Console.WriteLine();
foreach(string dinosaur in dinosaurs)
{
Console.WriteLine(dinosaur);
}
}

public void DiscoverByEnding(string Ending)
{
Predicate<string> dinoType;

switch (Ending.ToLower())
{
case "raptor":
dinoType = EndsWithRaptor;
break;
case "tops":
dinoType = EndsWithTops;
break;
case "saurus":
default:
dinoType = EndsWithSaurus;
break;
}
Console.WriteLine(
"\nArray.Exists(dinosaurs, \"{0}\"): {1}",
Ending,
Array.Exists(dinosaurs, dinoType));

Console.WriteLine(
"\nArray.TrueForAll(dinosaurs, \"{0}\"): {1}",
Ending,
Array.TrueForAll(dinosaurs, dinoType));

Console.WriteLine(
"\nArray.Find(dinosaurs, \"{0}\"): {1}",
Ending,
Array.Find(dinosaurs, dinoType));

Console.WriteLine(
"\nArray.FindLast(dinosaurs, \"{0}\"): {1}",
Ending,
Array.FindLast(dinosaurs, dinoType));

Console.WriteLine(
"\nArray.FindAll(dinosaurs, \"{0}\"):", Ending);

string[] subArray =
Array.FindAll(dinosaurs, dinoType);

foreach(string dinosaur in subArray)
{
Console.WriteLine(dinosaur);
}
}

// Search predicate returns true if a string ends in "saurus".
private bool EndsWithSaurus(string s)
{
if ((s.Length > 5) &&
(s.Substring(s.Length - 6).ToLower() == "saurus"))
{
return true;
}
else
{
return false;
}
}

// Search predicate returns true if a string ends in "raptor".
private bool EndsWithRaptor(String s)
{
if ((s.Length > 5) &&
(s.Substring(s.Length - 6).ToLower() == "raptor"))
{
return true;
}
else
{
return false;
}
}

// Search predicate returns true if a string ends in "tops".
private bool EndsWithTops(String s)
{
if ((s.Length > 3) &&
(s.Substring(s.Length - 4).ToLower() == "tops"))
{
return true;
}
else
{
return false;
}
}
}

/* This code example produces the following output:

Compsognathus
Amargasaurus
Oviraptor
Velociraptor
Deinonychus
Dilophosaurus
Gallimimus
Triceratops

Array.Exists(dinosaurs, "saurus"): True

Array.TrueForAll(dinosaurs, "saurus"): False

Array.Find(dinosaurs, "saurus"): Amargasaurus

Array.FindLast(dinosaurs, "saurus"): Dilophosaurus

Array.FindAll(dinosaurs, "saurus"):
Amargasaurus
Dilophosaurus
*/
``````
``````Imports System

Public Class DinoDiscoverySet

Public Shared Sub Main()
Dim dinosaurs() As String = { "Compsognathus", _
"Amargasaurus",   "Oviraptor",      "Velociraptor", _
"Deinonychus",    "Dilophosaurus",  "Gallimimus", _
"Triceratops" }

Dim GoMesozoic As New DinoDiscoverySet(dinosaurs)

GoMesozoic.DiscoverAll()
GoMesozoic.DiscoverByEnding("saurus")
End Sub

Private dinosaurs As String()

Public Sub New(items() As String)
dinosaurs = items
End Sub

Public Sub DiscoverAll()
Console.WriteLine()
For Each dinosaur As String In dinosaurs
Console.WriteLine(dinosaur)
Next dinosaur
End Sub

Public Sub DiscoverByEnding(Ending As String)
Dim dinoType As Predicate(Of String)

Select Case Ending.ToLower()
Case "raptor"
Case "tops"
Case "saurus"
Case Else
End Select

Console.WriteLine(vbNewLine + _
"Array.Exists(dinosaurs, ""{0}""): {1}", _
Ending, _
Array.Exists(dinosaurs, dinoType))

Console.WriteLine(vbNewLine + _
"Array.TrueForAll(dinosaurs, ""{0}""): {1}", _
Ending, _
Array.TrueForAll(dinosaurs, dinoType))

Console.WriteLine(vbNewLine + _
"Array.Find(dinosaurs, ""{0}""): {1}", _
Ending, _
Array.Find(dinosaurs, dinoType))

Console.WriteLine(vbNewLine + _
"Array.FindLast(dinosaurs, ""{0}""): {1}", _
Ending, _
Array.FindLast(dinosaurs, dinoType))

Console.WriteLine(vbNewLine + _
"Array.FindAll(dinosaurs, ""{0}""):", Ending)

Dim subArray() As String = _
Array.FindAll(dinosaurs, dinoType)

For Each dinosaur As String In subArray
Console.WriteLine(dinosaur)
Next dinosaur
End Sub

' Search predicate returns true if a string ends in "saurus".
Private Function EndsWithSaurus(s As String) As Boolean
' AndAlso prevents evaluation of the second Boolean
' expression if the string is so short that an error
' would occur.
If (s.Length > 5) AndAlso _
(s.ToLower().EndsWith("saurus")) Then
Return True
Else
Return False
End If
End Function

' Search predicate returns true if a string ends in "raptor".
Private Function EndsWithRaptor(s As String) As Boolean
' AndAlso prevents evaluation of the second Boolean
' expression if the string is so short that an error
' would occur.
If (s.Length > 5) AndAlso _
(s.ToLower().EndsWith("raptor")) Then
Return True
Else
Return False
End If
End Function

' Search predicate returns true if a string ends in "tops".
Private Function EndsWithTops(s As String) As Boolean
' AndAlso prevents evaluation of the second Boolean
' expression if the string is so short that an error
' would occur.
If (s.Length > 3) AndAlso _
(s.ToLower().EndsWith("tops")) Then
Return True
Else
Return False
End If
End Function
End Class

' This code example produces the following output:
'
' Compsognathus
' Amargasaurus
' Oviraptor
' Velociraptor
' Deinonychus
' Dilophosaurus
' Gallimimus
' Triceratops
'
' Array.Exists(dinosaurs, "saurus"): True
'
' Array.TrueForAll(dinosaurs, "saurus"): False
'
' Array.Find(dinosaurs, "saurus"): Amargasaurus
'
' Array.FindLast(dinosaurs, "saurus"): Dilophosaurus
'
' Array.FindAll(dinosaurs, "saurus"):
' Amargasaurus
' Dilophosaurus
``````

## 備註

Predicate<T>會傳回方法的委派`true`物件傳遞給是否符合委派中定義的條件。The Predicate<T> is a delegate to a method that returns `true` if the object passed to it matches the conditions defined in the delegate. 項目`array`個別傳遞給Predicate<T>、 移動中回溯Array、 的最後一個元素開始和結束的第一個元素。The elements of `array` are individually passed to the Predicate<T>, moving backward in the Array, starting with the last element and ending with the first element. 找到相符項目時，會停止處理。Processing is stopped when a match is found.