Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use the TryGetNonEnumeratedCount method of .NET 6

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains how to use the TryGetNonEnumeratedCount method of. NET 6. Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let Xiaobian take you to learn how to use the TryGetNonEnumeratedCount method of. NET 6.

I. Examples

Try the following code:

var b = new B();Console.WriteLine($@"{b.Count()}");var a = new A();Console.WriteLine($@"{a.Count()}");class A : IEnumerable{ public IEnumerator GetEnumerator() { throw new NotImplementedException(); } IEnumerator IEnumerable.GetEnumerator() { throw new NotImplementedException(); }}class B : A,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(); }}

You will find that b.Count() can execute, while a.Count() will report an error:

II. Principle

This is caused by LINQ's internal implementation.

For some collection types, if you cannot quickly determine the number of collection elements--for example, the Count property--calling Count() must enumerate the entire collection to determine the number of elements.

In some cases, enumeration can seriously affect program performance, such as using IQueryable.Count() under EF Core requires accessing the database to get all records to count.

Therefore, a more efficient way to count the number of elements in a sequence is to use the TryGetNonEnumeratedCount method, which returns true and returns the count as an out variable if it can be counted quickly.

Conclusion:

It is recommended that you always use the following format code to get the total number of elements of enumerable types:

if (! enumerable.TryGetNonEnumeratedCount(out var count)){ //Use other methods to get the number of elements} At this point, I believe that everyone has a deeper understanding of "How to use the TryGetNonEnumeratedCount method of. NET 6". Let's actually operate it! Here is the website, more related content can enter the relevant channels for inquiry, pay attention to us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report