前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >.NET6新东西---TryGetNonEnumeratedCount

.NET6新东西---TryGetNonEnumeratedCount

作者头像
喵叔
发布2021-12-27 08:41:59
4280
发布2021-12-27 08:41:59
举报
文章被收录于专栏:喵叔's 专栏

在.NET6中微软新增了 TryGetNonEnumeratedCount 方法,让我们可以计算可枚举类型元素的总数。到这里一定有读者会问:LINQ中已经包含了Count方法,为什么还要增加TryGetNonEnumeratedCount方法呢?要解决这个问题,我们先来看一段代码:

代码语言:javascript
复制
var u = new User<int>();
Console.WriteLine($@"{u.Count()}");
var e = new Employee<int>();
Console.WriteLine($@"{e.Count()}");
class User<T> : IEnumerable<T>
{
    public IEnumerator<T> GetEnumerator()
    {
        throw new NotImplementedException();
    }

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

class Employee<T> : User<T>, ICollection
{
    public int Count => 123456;

    public bool IsSynchronized => throw new NotImplementedException();

    public object SyncRoot => throw new NotImplementedException();

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

运行上面的代码会发现e.Count()能够执行,但是u.Count()报错,这是LINQ内部实现造成的。某些集合类型如果无法通过Count属性/方法确定集合元素数量的话,会严重影响程序性能,这是因为Count属性/方法必须枚举整个集合来确定元素数量,例如EF Core中使用IQueryable.Count()方法需要访问数据库获取全部记录才能计算总数。为了解决这个问题我们就需要使用TryGetNonEnumeratedCount方法,如果可以快速计数,该方法将返回true并将计数作为out变量返回。因此建议在代码中始终使用如下方式获取可枚举类型的元素总数:

代码语言:javascript
复制
if (!enumerable.TryGetNonEnumeratedCount(out var count))
{
    //more code
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2021/12/26 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档