.NET 6新特性试用 | TryGetNonEnumeratedCount

前言

.NET 6新增了TryGetNonEnumeratedCount方法,计算可枚举类型的元素总数。

LINQ不是已经有了Count方法吗,为什么还要画蛇添足呢?

Demo

尝试下列代码:

var b = new B<int>();
Console.WriteLine($@"{b.Count()}");

var a = new A<int>();
Console.WriteLine($@"{a.Count()}");

class A<T> : IEnumerable<T>
{
    public IEnumerator<T> GetEnumerator()
    {
        throw new NotImplementedException();
    }

    IEnumerator IEnumerable.GetEnumerator()
    {
        throw new NotImplementedException();
    }
}

class B<T> : A<T>,ICollection
{
    public int Count => 10086;

    public bool IsSynchronized => throw new NotImplementedException();

    public object SyncRoot => throw new NotImplementedException();

    public void CopyTo(Array array, int index)
    {
        throw new NotImplementedException();
    }
}

你会发现,b.Count()能够执行,而a.Count()会报错: 

701092119c697c442fcdcfde0493f43f.png

原理

这是由LINQ内部实现造成的。

对于某些集合类型,如果无法快速确定集合元素数量——例如Count属性,调用Count()则必须枚举整个集合以确定元素的数量。

在某些情况下,枚举会严重影响程序性能,比如EF Core下使用IQueryable.Count()就需要访问数据库获取全部记录才能计数。

因此,更高效地计算序列中的元素数,就是使用TryGetNonEnumeratedCount方法,如果可以快速计数,该方法将返回true并将计数作为out变量返回。

结论

建议你始终使用以下格式代码,去获取可枚举类型的元素总数:

if (!enumerable.TryGetNonEnumeratedCount(out var count))
{
    //使用其他方式获取元素数量
}

如果你觉得这篇文章对你有所启发,请关注我的个人公众号”My IO“

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值