Laravel  
laravel
文档
数据库
架构
入门
php技术
    
Laravelphp
laravel / php / java / vue / mysql / linux / python / javascript / html / css / c++ / c#

c# list find

作者:死亡之神   发布日期:2026-03-19   浏览:89

using System;
using System.Collections.Generic;

class Program
{
    static void Main()
    {
        List<int> numbers = new List<int> { 1, 2, 3, 4, 5 };

        // 使用 Find 方法查找第一个满足条件的元素
        int? result = numbers.Find(n => n > 3);

        if (result != null)
        {
            Console.WriteLine("找到的第一个大于3的数字是: " + result);
        }
        else
        {
            Console.WriteLine("没有找到符合条件的元素");
        }
    }
}

解释说明:

  • List<T>.Find 方法用于查找列表中第一个满足指定条件的元素。
  • 在这个例子中,我们定义了一个包含整数的列表 numbers
  • 使用 Find 方法和 lambda 表达式 n => n > 3 查找列表中第一个大于 3 的元素。
  • 如果找到了符合条件的元素,则输出该元素;否则输出提示信息表示没有找到符合条件的元素。

上一篇:c# task和thread区别

下一篇:c# int转string

大家都在看

c# 二进制

c# 创建目录

c# datatable group by

c# tcp client

c# type.gettype

c# sqlconnection

c# string.format 小数位数

.net和c#

c#获取系统时间

c#游戏开发

Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3

Laravel 中文站