Ienumerable<t> knowledge points in C #

Source: Internet
Author: User

1. Ways to extend Ienumerable<t>The Mys method is used to inherit the Ienumeralbe<t> interface.
  
 
  1. static class MySum {Must be a static class
  2. public static int Mys(this IEnumerable<int> value) { // 必须是静态方法
  3. int sum = 0;
  4. var ie = value.GetEnumerator();Returns a IEnumerator iterator by using the GetEnumerator () method
  5. while(ie.MoveNext()) {
  6. sum += ie.Current;
  7. }
  8. return sum;
  9. }
  10. }
I find it difficult to understand the value in the code above, and value is an instance of an interface that is declared by IE, and can be used in the Ie<t> method
2. Enable a class to iterate over LINQ and must have the class inherit the Ienumerable<t> interface
   
 
  1. class Person : IEnumerable<int> {
  2. public int _age;
  3. public IEnumerator<int> GetEnumerator() {
  4. yield return 1;
  5. yield return 2;
  6. yield return 31;
  7. yield return 4;
  8. yield return 5;
  9. yield return 6;
  10. }
  11. public int MyCount() {
  12. int sum = 0;
  13. var ie = this.GetEnumerator();
  14. while(ie.MoveNext()) {
  15. sum++;
  16. Console.WriteLine(ie.Current);
  17. }
  18. return sum;
  19. }
  20. IEnumerator IEnumerable.GetEnumerator() {
  21. throw new NotImplementedException();
  22. }
  23. }
And then how to use LINQ and foreach is a very simple thing to do.
   
 
  1. Person pp = new Person();
  2. varQQ=pp.Where(C=C< Ten).Select(C= New {type=C.GetType(),username=C.ToString()}); //Use an anonymous method to create a new collection that conforms to the Ie<t> interface
  3. // Console.WriteLine(qq)); qq是一个系统的匿名类型,没有MyConut方法,这里只是写出来,qq在点后无法找到MYCount方法,故写在此处方便以后查阅
  4. foreach (var item in qq) {
  5. Console.WriteLine(item.type+" "+item.username);
  6. }
  7. Console.WriteLine(pp.MyCount());
  8. Console.WriteLine(pp.Mys());
  9. Console.ReadKey();

finally see a summary of others, the level of people should understand deeper.  I'll post it for future reference.

1, a collection to support the traversal of the Foreach method, the IEnumerable interface must be implemented (that is, the IEnumerator object must be returned in some way).
2. IEnumerator object specifically implements the iterator (via MoveNext (), Reset (), current).
3, from the two interfaces of the choice of words, you can also see the difference: IEnumerable is a declarative interface, declares that the class implementing the interface is "enumerable (enumerable)", but does not describe how to implement the enumerator (iterator) IEnumerator is an implementation-type interface, IEnumerator object is a iterator.
4, IEnumerable and IEnumerator through the IEnumerable GetEnumerator () method to establish the connection, the client can pass IEnumerable GetEnumerator () Get IEnumerator object, in this sense, will GetEnumerator () as IEnumerator object factory method also can not be.
IEnumerator is the base interface for all enumerators.

Enumerators allow only the data in the collection to be read. Enumerators cannot be used to modify the underlying collection.

Initially, the enumerator is positioned before the first element in the collection. Reset also returns the enumerator to this location. At this location, calling current will throw an exception. Therefore, before reading the value of current, you must call MoveNext to advance the enumerator to the first element of the collection.

Current returns the same object before calling MoveNext or Reset. MoveNext sets current to the next element.

After being passed to the end of the collection, the enumerator is placed after the last element in the collection, and calling MoveNext returns FALSE. If the last call to MoveNext returns false, calling current throws an exception. To set the current to the first element of the collection again, you can call Reset and then call MoveNext.

The enumerator remains valid as long as the collection remains unchanged. If changes are made to the collection (such as adding, modifying, or deleting elements), the enumerator is invalidated and unrecoverable, and the next call to MoveNext or Reset throws a InvalidOperationException. If the collection is modified between MoveNext and current, then even if the enumerator is invalid, current will return the element it is set to.

Enumerators do not have exclusive access to the collection; Therefore, enumerating a collection is inherently not a thread-safe process. Even when synchronizing a collection, other threads can still modify the collection, which causes the enumerator to throw an exception. To ensure thread safety during enumeration, you can lock the collection during the entire enumeration, or catch exceptions that are thrown because of changes made by other threads.


Source: >  



From for notes (Wiz)

Ienumerable<t> knowledge points in C #

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.