C# 数组 - FindIndex() 方法

C# 教程C# 教程

C# 数组 - FindIndex() 方法更新于 2025/6/8 17:22:17

C# 数组 FindIndex() 方法在找到与指定谓词定义的位置匹配的元素后,返回数组或其部分中首次出现的索引。

语法

以下是 C# 数组 FindIndex() 方法的语法 -

just-code 复制代码
public static T? FindIndex (T[] array, Predicate match);
public static T? FindIndex (T[], Int32, Predicate match);

参数

此方法接受以下参数 -

返回值

如果找到与谓词匹配的索引,则此方法返回该索引;否则返回 -1。

示例 1:查找第一个偶数的索引

这是 FindIndex() 方法的基本示例,用于显示数组中偶数首次出现的索引 -

demo-code 复制代码
using System;
class Program {
   static void Main() {
      int[] Number = new int[] {1, 2, 3, 4, 5};
   
      // 查找第一个偶数的索引
      int evenN = Array.FindIndex(Number, num => num % 2 == 0);
   
      Console.WriteLine("Index of First Even number is: " + evenN);
   }
}

输出

以下是输出 -

notranslate 复制代码
Index of First Even number is: 1

示例 2:查找字符串的第一个索引

让我们创建一个示例,使用 FindIndex() 方法显示第一个以"c"开头的字符串的索引 -

demo-code 复制代码
using System;
class Program {
   static void Main() {
      string[] names = { "Dipak", "Rhaul", "Chhavi", "Charlie" };
      // 查找以"C"开头的索引名称
      int index_number = Array.FindIndex(names, name => name.StartsWith("C"));
      Console.WriteLine("Index of First name starting with 'C': " + index_number);
   }
}

输出

以下是输出 -

notranslate 复制代码
Index of First name starting with 'C': 2

示例 3:如果未找到匹配项会怎样

让我们看一下 FindIndex() 方法的另一个示例,以及它如何处理在数组中未找到索引的情况 -

demo-code 复制代码
using System;
class Program {
   static void Main() {
      int[] numbers = {1, 3, 5, 7};
      int index = Array.FindIndex(numbers, num => num % 2 == 0);
      Console.WriteLine("Index of first even number: " + index);
   }
}

输出

以下是输出 -

notranslate 复制代码
Index of first even number: -1

示例 4:查找符合条件的自定义对象的索引

在此示例中,我们创建一个自定义对象,该对象包含人员的姓名和年龄。我们使用 FindIndex() 函数显示第一个年龄大于 23 岁的人员的索引 -

demo-code 复制代码
using System;
class Person {
   public string Name {
      get;
      set;
   }
   public int Age {
      get;
      set;
   }
}
class Program {
   static void Main() {
      Person[] people = {
         new Person {
            Name = "Dipak", Age = 25
         },
         new Person {
            Name = "Karan", Age = 30
         },
         new Person {
            Name = "Pankaj", Age = 22
         }
      };
      // 查找第一个年龄大于 23 岁的人的索引
      int first_index = Array.FindIndex(people, person => person.Age > 23);
      Console.WriteLine("Index of First person older than 23: " + first_index);
   }
}

输出

以下是输出 -

notranslate 复制代码
Index of First person older than 23: 0
相关推荐
代码山河4 天前
基本类型转换:自动类型转换与强制类型转换
java·学习·架构·教程·面向对象·项目
Liuqy-055 天前
TCP与UDP
网络·网络协议·tcp/ip·udp·c
2601_967987377 天前
完整教程:Next.js项目演示(从零创建Next.js项目)Next.js入门实战
react·教程·前端开发·next.js·项目创建
代码山河8 天前
Python就业方向全解析:后端、爬虫、数据分析、AI你选哪个?
python·学习·架构·教程·面向对象·项目
Maynor9969 天前
ChatGPT Pro 额度档位怎么理解:5x / 20x / 50x 使用笔记
ai·chatgpt·教程
迪菲赫尔曼10 天前
【基础篇 08】让VS Code连接服务器:界面在本地,文件保存到远端
运维·服务器·vscode·yolo·论文·教程·ultralyt
SunkingYang11 天前
C++14 vs C++17:新特性对比与用法指南
c++·新特性·用法·c++14·示例·c++17
xcLeigh12 天前
Unity基础:Destroy与DestroyImmediate——正确销毁游戏物体的方式
游戏·unity·游戏引擎·教程
迪菲赫尔曼12 天前
【基础篇 02】文件、文件夹和路径:先把学习资料放对地方
yolo·计算机视觉·论文·教程·ultralyt
C++ 老炮儿的技术栈12 天前
机器人的坐标,像 XYZ 的坐标值,是用单精度浮点还是用双精度浮点?
c语言·c++·人工智能·单片机·c