You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C#前缀过滤数组方法问题:null参数异常不触发+过滤逻辑错误

问题分析与解决方案

一、传入null前缀时异常未触发的原因

你的方法用了yield return返回IEnumerable<string>,这类方法是延迟执行的——只有当调用方开始枚举返回的集合(比如用foreach遍历、调用ToList()/ToArray()等)时,方法体里的代码才会逐步执行。如果调用GetPrefixItems后没有操作返回的集合,null检查的代码根本不会被执行,自然不会抛出异常。

二、过滤逻辑不符合预期的原因

你写的data.All(prefix.Contains)完全偏离需求:

  • data.All(...)是判断集合中所有元素是否满足条件,而不是检查当前遍历的单个item
  • prefix.Contains(item)是判断前缀字符串里是否包含当前item,和需求的“item以前缀开头”逻辑完全相反
  • 没有处理“不区分大小写”的要求

三、修正后的代码

版本1:保留foreach+yield的写法

using System;
using System.Collections.Generic;
using System.Linq;

namespace Enumerable
{
    public class EnumerableManipulation
    {
        public IEnumerable<string> GetPrefixItems(IEnumerable<string> data, string prefix)
        {
            // 立即检查前缀null,抛出带参数名的规范异常
            if (prefix == null)
            {
                throw new ArgumentNullException(nameof(prefix));
            }

            // 空前缀直接返回所有项
            if (string.IsNullOrEmpty(prefix))
            {
                foreach (var item in data)
                {
                    yield return item;
                }
                yield break;
            }

            // 不区分大小写检查前缀,同时处理item为null的情况
            foreach (string item in data)
            {
                if (item?.StartsWith(prefix, StringComparison.OrdinalIgnoreCase) == true)
                {
                    yield return item;
                }
            }
        }
    }
}

版本2:用LINQ简化实现

using System;
using System.Collections.Generic;
using System.Linq;

namespace Enumerable
{
    public class EnumerableManipulation
    {
        public IEnumerable<string> GetPrefixItems(IEnumerable<string> data, string prefix)
        {
            if (prefix == null)
                throw new ArgumentNullException(nameof(prefix));
            
            if (string.IsNullOrEmpty(prefix))
                return data;
            
            // LINQ延迟执行,同时完成前缀检查
            return data.Where(item => item?.StartsWith(prefix, StringComparison.OrdinalIgnoreCase) == true);
        }
    }
}

关键改动说明

  1. 异常规范:给ArgumentNullException传入nameof(prefix),明确指出是哪个参数为null,符合.NET开发规范
  2. 空前缀处理:直接返回原集合,满足需求
  3. 正确的前缀检查:用string.StartsWith配合StringComparison.OrdinalIgnoreCase实现不区分大小写的前缀匹配,比ToLower()/ToUpper()更高效,还能避免文化差异导致的匹配错误
  4. 空值安全:用item?.StartsWith处理item为null的情况,避免抛出NullReferenceException
  5. 立即执行null检查:不管用哪种写法,null检查都放在方法最开头且是立即执行的,不会因为延迟执行被跳过

内容的提问来源于stack exchange,提问作者Akzhol

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.24 08:09:17