本文整理匯總了C#中System.Array.FindLast<T>方法的典型用法代碼示例。如果您正苦於以下問題:C# Array.FindLast<T>方法的具體用法?C# Array.FindLast<T>怎麽用?C# Array.FindLast<T>使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類System.Array
的用法示例。
在下文中一共展示了Array.FindLast<T>方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C#代碼示例。
示例1: Main
//引入命名空間
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;
}
}
}
輸出:
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