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

What should be paid attention to when using CLR in C #

2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly shows you "what you need to pay attention to when using CLR in C#", the content is simple and clear, and I hope it can help you solve your doubts. Let me lead you to study and learn this article "what you need to pay attention to when using CLR in C#".

1. Performance problems of foreach of C # CLR

The implementation of foreach (string s in rows) {foo (s);} is:

IEnumerator e = rows.GetEnumerator (); try {string s; while (e.MoveNext ()) {s = (String) e.Current; foo (s);}} finally {IDisposable d = e as IDisposable; if (d! = null) d.Dispose ();}

The e.MoveNext () and e.Current methods are called at each step; most of the time, it is entirely possible to optimize to a single call. Obviously, this has an impact on performance. Although foreach makes separate optimizations for arrays (compiled into for loops), this is worth noting.

So, how to do it faster?

For Collection such as List, we can use ForEach (Actionaction), FindAll (Predicatematch), ConvertAll (Converterconverter) and other methods. They are faster, but not all classes that implement IEnumerable provide them.

LINQ pursues compatiblity, not performance. Therefore, the implementation of LINQ completely adopts foreach. It's worth noting.

2. The implementation principle of yield of C # CLR.

The yield keyword is usually used when implementing an object that supports IEnumerable, so that when foreach traverses the object, it can lazy evaluation. For example:

Class MyCollection: IEnumerable {private string s;... Public IEnumerable GetEnumerator () {for (int item0; i2)

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