本文整理汇总了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